-
-
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
Allow x[] for get(x::Nullable) #18766
Conversation
""" | ||
get(x::Nullable) | ||
|
||
Alias for `getindex(x::Nullable)`. |
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.
I preferred the old docstring in which y
was marked as optional.
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.
Personally, I found that docstring confusing. (Having both x
and y
be possibly missing was confusing for me.) Perhaps we can wait for more feedback from others.
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.
At least, it should give direct information instead of redirecting to another docstring.
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.
How is this?
Not a fan of this syntax, to be honest... I think if we want a neater syntax for this case, my preferred option would be to wait until #1974 is implemented, and then use |
I was not expecting this change to be controversial. I suppose a [decision] tag would be warranted, then. There are two reasons that I prefer this syntax: cleanliness and consistency. Note that Furthermore, postfix notation is quite natural for accessing values. (That's why most languages use Maybe this whole operation should be rethought. Mostly I stumble upon it in conditions like if isnull(x)
# blah
else
foo(get(x))
end which I personally find very ugly to read and strictly inferior to if isnull(x)
# blah
else
foo(x[])
end Do many of you see that pattern frequently in your use cases? but perhaps this entire idiom is quite inferior. It would be nice to have Scala-style pattern matching on Nullable, which would have improved type safety and other general benefits. But that's a separate issue. |
|
@TotalVerb I completely agree that 1) postfix notation is the way to go here and 2) that these kind of |
I actually disagree that postfix notation is the way to go here--I think it makes it harder to read, plus I fail to see an issue with |
I've been thinking hard about an alternative, Swift-inspired syntax for this. Something like ??(x::Nullable) = !x.isnull # or x.hasvalue
!!(x::Nullable) = x.value (I'd prefer x ?? foo(x) : default_value (where the parser lowers that to if isnull(x)
default_value
else
foo(get(x))
end I'm not sure if this stuff has been discussed before or attempted somewhere? EDIT: and of course the binary |
@andyferris More syntax for nullables would be nice, but See also #15174 for another kind of convenience syntax. |
get(x::Nullable) | ||
|
||
Attempt to access the value of `x`. Throw a `NullException` if the value is not | ||
present. |
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.
Should probably mention the x[]
syntax here.
Even with dot overloading, it is not at all clear to me that 👍 for |
Yeah, I have to say that I like |
To elaborate, I view a |
On that note some of us have been looking for something like |
@andyferris I believe that is #18379. |
Thanks! Perfect |
02de8d0
to
c550628
Compare
@@ -71,7 +71,7 @@ getindex(x::Nullable) = isnull(x) ? throw(NullException()) : x.value | |||
get(x::Nullable) | |||
|
|||
Attempt to access the value of `x`. Throw a `NullException` if the value is not | |||
present. Equivalent to `getindex(x::Nullable)`. | |||
present. Equivalent to `getindex(x::Nullable)`, which can be spelt `x[]`. |
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.
Since most if not all of the documentation is in American English, you should probably use "spelled" instead of "spelt." I'd say something along the lines of
Equivalent to
getindex(x::Nullable)
, which can alternatively be written asx[]
.
""" | ||
get(x::Nullable) | ||
|
||
Attempt to access the value of `x`. Throw a `NullException` if the value is not |
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.
throws?
I should say that I would really welcome anything that makes working with |
Looking back over this, I actually really like this, so I'm adding it to DataValues.jl. So, if anyone wants to use this, just wait until queryverse/DataValues.jl#20 is merged. Main use case right now would be in Query.jl, i.e. you should be able to use this in any Query.jl query. |
If we'd still like this change (which I am still in favor of), chances are that it will be outside Base along with the new Nullable. Thus, closing. |
No other
get
function has these semantics, and thex[]
is better syntax anyway.Note that parallel from
to
get(dict, key, default)
versus
to
get(nullable, default)