-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
WIP/RFC: add 2-argument get
returning Union{Nothing,Some}
#34821
base: master
Are you sure you want to change the base?
Conversation
I have long wanted this and am happy to see this discussed after the old attempt with nullables in #18211. As a possible other meaning of two-args
This is not the place to discuss such an API, but in this case there is no function which throws when no key is found, so I though that |
Reflecting on @rfourquet's comment I do have to say, there is a semantic that kinda works for dictionaries and a possible multi-dict. Instead of One neat thing about this as a form "nullable" type is that many operations just fall out. "Optional chaining" can be done via broadcasting. "Nullish coalescing" is somewhat like three-argument Anyway, I understand we settled on the (@rfourquet Multidict could have |
Another strange idea: syntax sugar for two-argument |
This is a neat syntax but I think it'd be incompatible with Having said that, I cannot help mentioning that there is a nice generalization of this in the case dict?[key] = something(dict?[key], 0) + 1 can be lowered to modify!(dict, key) do val
something(val, 0) + 1
end where The signature of |
jl_value_t *jl_eqtable_get1(jl_array_t *h, jl_value_t *key, int *found) | ||
{ | ||
void **bp = jl_table_peek_bp(h, key); | ||
if (found) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if (found) |
Maybe another point to consider: Although I'm not convinced of the need for consistency here, as |
We have that behaviour in |
@rfourquet For maybepop!(dict, key) = modify!(_ -> nothing, dict, key) This returns a @andyferris IIUC, @rfourquet is pointing out the similarity of the existing APIs I don't think we can do anything about |
I think this would break quite some code. For instance, it will break Setfield.jl hard. I would guess we are not the only ones that overloaded |
The life of a type pirate is fraught with peril! |
I guess so. Just to clarify |
To be clear, I don’t have a strong opinion either way here and we should avoid causing undue breakage. Mostly just a joke. |
If we want I think @andyferris is designing "multi-dimensional" dictionary. So, thinking about multi-dimensional key for "2-arg |
Another possible name for this would be |
I'm probably being silly, but what's the point of this over I'm imagining code like: if haskey(dict, key)
value = dict[key]
# do something
else
# do something on failure
end versus x = get(dict, key)
if x !== nothing
value = something(x)
# do something
else
# do something on failure
end There's one more hash going on in the first example, but maybe that could be optimised away? |
Unfortunately, optimizing a repeated hash lookup away seems unlikely to be feasible. There's just too much un-inlined code and Julia doesn't have the pure-functional semantics that might enable the detection of bulk repeated work. (It would also be nice to have an interface that presents the efficient path for all sorts of dictionaries - less round trips to a remote database, for example). |
In terms of API, perhaps this would be more consistent to call |
I like |
I'm suggesting to use |
This form is a bit more general, and would eventually allow us to eliminate the loathsome
secret_table_token
.Obviously incomplete; we should first discuss whether we want this.