-
-
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
Get dict value as a nullable #13055
Comments
Very reasonable idea. |
Ref #12157. Not necessarily a dup of that but should be solved by the proposal there. |
Does it have to be one or the other (this or #12157)? I think this would be a very nice capability to have, no matter what. |
If |
Don't think that would play nicely with ternary syntax. Though resolving #4233 (nicest approach proposed so far would be a general macro |
Indeed, hence conditioning the suggestion on getting Fun aside, it is currently possible to use the
|
I think this could be spelled julia> methods(get, Tuple{Associative,Any})
0-element Array{Any,1}
julia> methods(get, Tuple{Associative,Vararg{Any}})
5-element Array{Any,1}:
get(t::ObjectIdDict, key::ANY, default::ANY) at dict.jl:326
get{K,V}(h::Dict{K,V}, key, default) at dict.jl:722
get{K}(wkh::WeakKeyDict{K,V}, key, default) at dict.jl:834
get(::Base.EnvHash, k::AbstractString, def) at env.jl:80
get{K,V}(pq::Base.Collections.PriorityQueue{K,V,O<:Base.Order.Ordering}, key, deflt) at collections.jl:227
julia> methods(get, Tuple{Any,Any})
1-element Array{Any,1}:
get{T}(x::Nullable{T}, y) at nullable.jl:33 |
Ahah, thank you for these resources. I'll be curious to see if requiring whitespace for the ternary operator and thereby freeing up |
I think when the original idea to allow |
Hmm. Is requiring whitespace between a condition and the ternary operator tantamount to nixing the latter altogether? I understand that such a requirement may be a particularly annoying gotcha, but beyond that I must be missing something. |
I think @StefanKarpinski may be softening his stance given his growing appreciation of the centrality of nullable types in many practical settings. |
Too bad, the ternary operator is one of the things that is always confusing to new programmers, and uses up two ASCII characters (a very limited resource). |
Death to the ternary operator. |
It really does seem out of place, with some of the rest of the Julia syntax being easy to read/understand compared to C/C++/Java/JS world. Maybe I need to define something like |
-1000000. Must... use... ternary... operator... |
I've said it before, but I'd still be in favor of a |
Actually, you already can do a 1 line if construct in Julia, ternary operator is just extra unnecessary syntax. |
or without the
Though I think python's |
I just wasn't sure if it would always work without the Julia also has some rough edges because of things like |
By extension, such a function would also make sense for arrays. This suggests a syntax that extends the x = d[key?]
x = d[?key]
x = d[key]?
x = d?[key] Regarding parsing -- it might be a nightmare to implement, but all except the third one are unambiguous (i.e. do not conflict with the current |
I'd worry that the 1st and 4th might have problems, what happens when key is an expression? |
The syntax Of course, if you're changing how |
julia> b?[1]:2
1-element Array{Int64,1}:
1 That would attempt to make a Range if the parsing would change. Even if we allow |
I'll clarify my original remarks. There are at least a few kinds of situations I can think of in which a method returns a I don't think |
This returns a Nullable value corresponding to the key (or null). Includes specialization for dictionaries. Fixes JuliaLang#13055
This issue has been open for almost a year now. @JeffBezanson I hope this idea still seems as "reasonable" as it did last year. Please let me know if not. Without straying into the larger questions regard special syntax for Nullable types and/or functions that return Nullables, I went ahead and implemented the fix I initially proposed. (See #18211) Could someone please review/decide? |
This returns a Nullable value corresponding to the key (or null). Includes specialization for dictionaries. Fixes JuliaLang#13055
This returns a Nullable value corresponding to the key (or null). Includes specialization for various dictionary types. Fixes JuliaLang#13055
This returns a Nullable value corresponding to the key (or null). Includes specialization for various dictionary types. (Fix bug in priority queue test.) Fixes JuliaLang#13055
FWIW, here's a review of how several languages handle looking up a non-existent key in a dict (see in particular Appendix C): |
Nullable is removed from Base. Not moving this to Nullables.jl because it seems like this needs to be a Base feature in order for it to be useful. |
The issue itself is still relevant though. See PR #25131 for a similar change (merging |
If we were to have that, perhaps it would be best called |
I can open a new issue for this, but this seem the closest one to what I want. I want to add to
Basically, this avoids the double lookup problem that sometimes pop up in Discourse (see: https://discourse.julialang.org/t/avoiding-double-lookup/78636). People end up using My reasoning for applying
Another name, like |
I need a good way to both test for a value in a dictionary and get that value, without resorting to one of the following fallbacks, all of which are somewhat undesirable:
get(dict, key, specialnullval)
-- problematic because I must either use a separate type to represent null (inefficient due to the type-variadic return), or I must have some special way to represent null within the value type of the dictionary.haskey(dict, key)
followed bydict[key]
-- problematic because it looks up the key twice when it exists.try k = dict[key] catch ...
-- problematic because it relies on exceptions when the key is not present.It seems that all of this can be solved my making use of Julia v0.4's
Nullable
type, with something like the following:I am proposing that this be included as part of
base/dict.jl
, possibly under a different/better name.Is there some other/better way to do this?
The text was updated successfully, but these errors were encountered: