Add get with default#26
Conversation
| function get(val::ScopedValue{T}, default::T) where {T} | ||
| scope = current_scope()::Union{Nothing, Scope} | ||
| scope === nothing && return default | ||
| scope = scope::Scope | ||
| return Base.get(scope.values, val, default) | ||
| end |
There was a problem hiding this comment.
The ::Union{Nothing, Scope} is likely unecessary (and might even be slower).
There was a problem hiding this comment.
The ::Union{Nothing, Scope} is how this is called upstream. I've removed it in this PR, but which is right?
|
Without requiring the ScopedValue to have a Union type.
aa724f3 to
28ca05a
Compare
Done.
Added a test. The benchmark does not look current for 1.12 (it uses
Cc: @KristofferC and @topolarity to see if there is anything objectionable to adding this.
How would you use the default in the scoped value in this case? |
That should be returned instead of the provided default. The default value is the current value, and get with default should only return the user-provided default if there is no current value. |
b543e78 to
6026aab
Compare
|
Hey, sorry we left this PR dangling for so long. I understand there was some discussion about whether we should make Can we re-review the PR here? It seems like a nice API addition, independent of any of the impacts on the allocation / performance. :) |
Per this comment.
Our hope, as reflected in one of the added tests, is that this will be non-allocating (unlike the basic
get).