public R GetOrAdd(T key, Func <T, R> addFunc) { var res = dict.GetOrAdd(key, _ => NewRef(key, addFunc)); OnFinalise <R> target = null; if (res.TryGetTarget(out target)) { return(target.Value); } else { var upd = NewRef(key, addFunc); res = dict.AddOrUpdate(key, upd, (_, __) => upd); if (res.TryGetTarget(out target)) { return(target.Value); } else { // This is a best guess of why the target can't be got. // It might not be the best approach, perhaps a retry, or a // better/more-descriptive exception. throw new OutOfMemoryException(); } } }
public OptionUnsafe <R> TryGetValue(T key) { WeakReference <OnFinalise <R> > res = null; OnFinalise <R> target = null; return(dict.TryGetValue(key, out res) ? res.TryGetTarget(out target) ? SomeUnsafe(target.Value) : None : None); }