-
-
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
support dots for unary operators #20249
Conversation
Would also close #20039. |
Unary operators being prefix like normal function calls, having the dot precede rather than follow the operator seems a bit jarring. Being able to say "dots follow identifiers in prefix calls, and precede identifiers in infix calls" seems nice (rather than "dots follow identifiers in prefix calls --- except for a privileged set of unary operators where the dot precedes the operator --- and precede infix identifiers".) If anything, changing the position of dots attached to infix operators to match dot position elsewhere seems a better step, the rule then simply being "dots follow identifiers in dot calls". Thoughts? Thanks! |
@Sacha0, for |
Nullable(1) === Nullable(6) | ||
@test Nullable(1) .+ Nullable(1) .+ Nullable(1) .+ Nullable{Int}() .+ | ||
Nullable(1) .+ Nullable(1) |> isnull_oftype(Int) | ||
@test (Nullable(1) .+ Nullable(1) .+ Nullable(1) .+ Nullable(1) .+ Nullable(1) .+ |
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.
is this change related?
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 be good to merge? |
I would appreciate a bit more time to think this through and write a response. Best! |
I'm not clear what there is to consider – this should definitely work, for the sake of consistency. Sure, we may want another, nicer syntax for it, but this seems uncontroversial. |
We should mention this interpretation in the documentation if it is now official. (I remember this interpretation being provided by While that interpretation makes Do you think a user familiar with the
How is this ambiguity different from e.g. the former
Is that not an equally strong argument for using
Could you expand?
The apparent inconsistency between |
This kind of relates to an issue I stumbled upon yesterday. I needed to pass |
@Sacha0, I'm not sure what you mean by an "official interpretation". The syntax is documented, but how you want to describe it is up to you. Technically, of course, It sounds like you are arguing to rename Given that The |
@StefanKarpinski, I think that in 0.7 or 1.0 or whatever we could easily make |
To be precise, in #8450 we settled on |
(Note that |
It's pretty frustrating not to be able to write e.g. |
@stevenj where is the rational for |
@GlenHertz, it was discussed at length in #8450 and #15032. I don't think we're looking to re-open the debate, nor do we decide these things by votes. Regarding readability, there is the possibility of an |
@stevenj I don't mean to be rude as I'm not a developer. This is an amazing feature. From what I could tell the main issue was parsing ambiguities like |
Not sure what you mean about "when writing a macro". But can we confine this PR to discussion about what to do with unary operators in 0.6? |
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 approve of this it seems like a logical step given everything else. If we want to discuss bigger changes elsewhere, that's fine but I don't really think that affects whether we go ahead with this PR or not.
Separate from the discussion above, how do operators that are both unary and binary behave with this PR? That is, what does |
Not sure if it is good time to add a comment to this interesting discussion, but I would like to report my practice of broadcasting unary operators over a tuple, which I believe this PR tries to implement. Just in case it helps future discussion. Even before this PR, I have been already broadcasting unary operators over a tuple. The trick is to do julia> (-).((1,2,3))
(-1,-2,-3)
julia> (!).((true,false,true))
(false,true,false) Because of the formal similarity with this already working |
@wsshin, the reason that But the whole point of defining something as a unary operator is that the caller is not forced to use parentheses. If you require parentheses, it might as well not be an operator at all. |
@stevengj, thanks for explaining why However, I think the point I wanted to raise is still valid: Let me ask this question. Suppose we have the dot syntax for broadcasting a function as implemented now, but don't have any element-wise operators defined. In other words, we have A related question is this. Which syntax is more fundamental syntax to Julia: the dot syntax for broadcasting a function, or the element-wise operators inherited from MATLAB? (Because I think the dot syntax for broadcasting is more fundamental to Julia, in the previous paragraph I considered the case where we have the dot syntax but don't have MATLAB-like element-wise operators.) If one thinks the dot syntax for broadcasting is more fundamental, appending a dot to operators/functions it belongs to seems like a design to follow. (I know there is an interpretation that the dot in Maybe I prefer If we choose (And I don't mind having |
The bottom line (for this PR) is this:
Supporting |
@Sacha0 has a valid point there. We have: julia> (1,4).-(1,2)
(0,2) This would strongly suggest we want: julia> .-(1,2)
(-1,-2) That is, the (unary) julia> .-(1,2)
-1 That is, |
@martinholters, that is a parsing ambiguity, but the behavior is not changed by this PR. (Note that #20321 will somewhat lessen the need for unary dotted operators, since you will simply be able to do |
But should it? If |
@martinholters, it's ambiguous; either one could be correct, like several other space-sensitive parsing ambiguities in Julia. My feeling is that the current behavior is fine, and I don't think this usage will be common anyway. Leaving the behavior as-is also has the advantage of not making this PR a breaking change. |
True. |
This PR implements dotted versions of all the unary operators, which as usual convert to fusing broadcast operations. (Closes #14161.)