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
Either make assignments a statement type or allow 'my' declarations in expressions #279
Comments
Adding another one to the "Advantage of turning assignment into a statement type": two other issues (#163 and #237) have identified "assignment precedence" as something that needs to be special-cased. But if assignment weren't actually part of expressions at all, but something outside of it, that concern wouldn't exist in the first place. |
Adding a dubious one to the "Disadvantages of turning
Though I can certainly argue this one both ways. It looks quite reminiscent of annotated parameters, for example. Also, I'm not 100% convinced we even want annotations on |
I'm more and more leaning to making assignments a statement type. Assignments are weird. As an operator they're easily a macro, or maybe even "I give up, let's just make this a magical primitive". By the way, it's interesting to think how the landscape in #214 changes if we change assignments to be a statement type. |
I just pushed up the branch "assignment-statement" — still a work in progress, but what it does is just exploratorily replace There seems to be a parser ambiguity somewhere. The new parsing rule looks like this:
( Anyway, I didn't get this to work for something as simple as Need to investigate further. |
Well! This is timely: PEP 572. |
I want to make progress on this one, so let's tally things up so far. (I also have a personal favorite, but I'd like to see the same side fall out "objectively".) Advantages of making assignment a statement
Advantages of making
|
Another advantage of making When I first implemented Then, inspired by IntelliJ's Java PSI, I managed a simplification in which only the thing assigned is stored, not an expression containing the assignment. Obvious in retrospect. (Note also how neatly the semipredicate problem is solved in this case. The expression always contains either a Qtree for an expression, or Making In my view, that's so neat and explainable that it counts as an advantage on its own. |
#160, reluctant as it is, would also count as a vote in favor of |
See #279 for reasons. In the end this was a smaller change than I feared it would be. Having made it, I'm also more convinced (despite initial misgivings) that it's a simpler model. (In particular, it used to be that assignment semantics resided "inside" the `my` statement semantics. Now it's all on the outside. The `my` term, at runtime, is completely transparent and behaves exactly like the identifier it declares.) It also doesn't hurt that this simpler model is also somewhat richer than the old one. :) Closes #279.
See #279 for reasons. In the end this was a smaller change than I feared it would be. Having made it, I'm also more convinced (despite initial misgivings) that it's a simpler model. (In particular, it used to be that assignment semantics resided "inside" the `my` statement semantics. Now it's all on the outside. The `my` term, at runtime, is completely transparent and behaves exactly like the identifier it declares.) It also doesn't hurt that this simpler model is also somewhat richer than the old one. :) Closes #279.
See #279 for reasons. In the end this was a smaller change than I feared it would be. Having made it, I'm also more convinced (despite initial misgivings) that it's a simpler model. (In particular, it used to be that assignment semantics resided "inside" the `my` statement semantics. Now it's all on the outside. The `my` term, at runtime, is completely transparent and behaves exactly like the identifier it declares.) It also doesn't hurt that this simpler model is also somewhat richer than the old one. :) Closes #279.
See #279 for reasons. In the end this was a smaller change than I feared it would be. Having made it, I'm also more convinced (despite initial misgivings) that it's a simpler model. (In particular, it used to be that assignment semantics resided "inside" the `my` statement semantics. Now it's all on the outside. The `my` term, at runtime, is completely transparent and behaves exactly like the identifier it declares.) It also doesn't hurt that this simpler model is also somewhat richer than the old one. :) Closes #279.
See #279 for reasons. In the end this was a smaller change than I feared it would be. Having made it, I'm also more convinced (despite initial misgivings) that it's a simpler model. (In particular, it used to be that assignment semantics resided "inside" the `my` statement semantics. Now it's all on the outside. The `my` term, at runtime, is completely transparent and behaves exactly like the identifier it declares.) It also doesn't hurt that this simpler model is also somewhat richer than the old one. :) Closes #279.
According to this text (page 70:10; strange numbering scheme), C++98 introduced being able to declare variables in |
Just a thing that keeps bothering me. A very subtle inconsistency.
In Perl 5 and Perl 6, assignment is an operator, though admittedly a fairly weird one (due to the lvalue semantics). In Python, assignment is a statement type.
007 sides with the Perls on this one, and that in itself has never bothered me much.
But 007 ends up siding with Python on a related feature, and that's where the inconsistency is:
my
declarations in 007 are a statement type, unlike in Perl 5 and Perl 6:Now, things are not identical syntactically in Python, which makes this inconsistency a little harder to spot: Python does not have a variable declaration keyword as such. (And I think that's a mistake, but that's another story.) Instead, the first assignment to a name in a given scope sort of also counts as a declaration. The actual syntax in Python for the above would be
print(a = 5)
, except that here thea =
means that we're trying to pass a (nonexistent) keyword argument toprint
.Anyway. Maybe we should go one way or the other. I haven't decided which.
my
declarations from a statement type to an expression type. (More a term than a prefix, I think.)So far I can only list the advantages of either, as far as I can see.
Advantage of turning
my
into an expressionIt would allow the following idiom:
Which we currently have to write like this:
It's a small thing, but I'm generally in favor of colocating declarators with the first use of a variable. Having to declare it on top of the loop without initializing it... feels like a small inconvenience.
(I don't think the scope of
c
should change just because we declare it in thewhile
condition, though. A contrived case could perhaps be made that it should... but I say it's outside the braces of the loop (and it's not a parameter), so it makes sense that it still be visible after the loop block too.)Disadvantage of turning
my
into an expressionWe wouldn't be able to do #199.
Advantage of turning assignment into a statement type
Simplicity would be the main advantage here. It's more consistent with how
my
already works in the language.Note that we can still (like Python) allow chained assignments
a = b = 42;
. That's just part of the new statement syntax. I think we'd say no tomy a = my b = 42;
on grounds of sanity. (Butmy a = b = 42;
would presumably be OK.)Disadvantage of turning assignment into a statement type
I'm struggling to even express the
while
loop idiom when assignment is a statement type. Something like this, I guess:You may or may not prefer the duplication to the increased complexity of assignment expressions.
(And Perl 5 has a
continue
block after loops to handle the case where we have earlynext
statements inside the loop, but want to do the same kind of "increment" logic between iterations.)The text was updated successfully, but these errors were encountered: