-
-
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
make &x sugar for RefValue(x) #27608
base: master
Are you sure you want to change the base?
Conversation
Out of curiousity, why is this lowering logic instead of using a standard unary operator? Also, have we considered how this relates to all the |
Following up on the discussion in #27563, my gut instinct is to be a little defensive against having (I guess I feel we could afford to be a bit more creative and not pile meanings into the same symbol.... on that note I’d honestly prefer to move Boolean and bitwise operations to English and double down on references in a serious way - I’m thinking we could eventually in the distant future even support some Pony or Rust type of stuff, but that’s surely an aside!). |
Technically, the parser is treating it as a "syntactic" unary operator. I guess your question is, why is it parsed as a special AST node rather than as a function call? It all boils down to wanting to distinguish between the programmer writing Basically, if we want A separate question is whether we want |
test/core.jl
Outdated
@@ -5742,7 +5742,7 @@ function constant23367 end | |||
let | |||
b = B23367(91, A23367(ntuple(i -> Int8(i), Val(7))), 23) | |||
@eval @noinline constant23367(a, b) = (a ? b : $b) | |||
b2 = &b[] # copy b via field assignment | |||
b2 = (&b)[] # copy b via field assignment |
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 the precedence of &
be higher than that of []
?
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 would vote no, but I think that's mostly the C/C++ roots in me than any principled argument.
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 was speculating in #27563 (comment) that maybe &a[i]
could (in the future) be convenient syntax a reference to the i
th element of a::Array
.
It looks pretty good to me. I still really wish this gave us a concise syntax for operations like |
I realized that I've said a lot, but not this: I really like the idea of using |
Back when we introduced APL indexing, I was convinced we'd need a syntax to preserve leading scalar dimensions in indexing — and thought the Now reductions dropping/keeping dimensions is definitely a more common issue, but unlike indexing, the shape of the |
Yeah, not a bad idea. In any case, carry on. The arguments for using
Having syntax here is useful because these all create different kinds of things: |
Right now, If |
Right, I'm proposing that we change that. |
Just to be clear, are you proposing a change in the parsing, or just in the lowering? i.e. we could keep the parsing of Or we could parse And I'm not sure what to do about |
I would guess that both
I find this to be a very interesting question. We even say that Julia bindings have reference semantics so I sometimes get my head in a spin as to what exactly I'm guessing we want The other thing to consider is that the dot syntax refers to properties not fields now, which is a whole another kettle of fish. You might have a property that you can read and write to, but which you can't get e.g. a pointer to. Is a |
In this PR, the lowering is completely independent of the |
In these proposals, |
I should clarify that they can't behave the same with respect to mutation, but they can be the same in all other ways. |
It seems to me that these are different concepts.
Exactly. The question is, which concept should |
But those would be the same thing as far as broadcasting is concerned, right? |
It should be the same on the RHS, but may be different on the LHS? |
The proposed |
Parsing The proposed behavior of I guess what I'm getting at here is that I don't find writing |
If I'm not opposed to lowering
|
Yes, I get that, but I feel like it's a weak motivation for using some very prime syntax. |
I agree with what Stefan is saying. This kind of syntax seems valuable to solve some of our more general reference problems. I have always been slightly uneasy about (ab)using If all this is just ugliness from |
While I really like the syntax, it is somewhat problematic with respect to its C connotations: A typical use could look like
This has the same symbol and typing behavior as the C addressof, with entirely different mutation semantics: In a different scenario, we could have Can't we take a different unary ascii for this and use |
I don't see how that's so problematic — in your example, We also just don't have many options. It's gotta be significantly shorter than That said, since this is "just" a shorthand we could consider unicode. |
The confusing issue with C is that, with this, one would use In C, the resulting pointer My example was bad, since On the other hand, the special parsing is probably necessary: I could see code using |
For syntax as nice as this, we should spend some more time thinking about how references can/should work more generally. For example |
On triage it came up that having |
We discussed this on triage. Points that were discussed
|
As suggested in #27563. Note that
&x
was already parsed as a special expression nodeExpr(:&, :x)
, which previously was used for a deprecatedccall
syntax. So all this PR does is to change the lowering toRefValue(x)
(eliminating the deprecation).This is especially useful for "scalarizing" arguments in dot calls (it also works with
@.
). For example:I updated the Base, stdlib, and test code to use the
&
syntax and it looks pretty natural to me.To do (assuming the consensus is in favor):