Proposal: sequence expressions #72
Replies: 10 comments
-
Thoughts:
|
Beta Was this translation helpful? Give feedback.
-
@DavidArno re: silly programs That's why I wrote that the alternative is to turn blocks into expressions. This upends every single tree node hierarchy in Roslyn, so the cost-to-benefit ratio is not attractive, but it's theoretically doable. |
Beta Was this translation helpful? Give feedback.
-
Yep, saw that. Still thinking through the implications though, so I didn't comment on it. I really like that idea, but feel there's got to be a downside that I haven't thought of yet. 😀 |
Beta Was this translation helpful? Give feedback.
-
@DavidArno re: scope leakage |
Beta Was this translation helpful? Give feedback.
-
Good point; I'd forgotten about that. There are issues with it, as the comments discuss, but it's a good starting point for discussion at least. |
Beta Was this translation helpful? Give feedback.
-
@qrli as implemented right now, statements do not leak any declaration outside the scope, I copied the scoping rules from blocks. I am not sure if the terminal expression leaks anything (it shouldn't), but I finally passed the basic happy path test at 2am, so I was in no state to investigate further. It can either leak or contain pattern and out variables, it's a design and implementation issue, not a blood-brain barrier. |
Beta Was this translation helpful? Give feedback.
-
I think a more realistic alternative to this proposal would be local functions. |
Beta Was this translation helpful? Give feedback.
-
Is dotnet/roslyn#6182 off the table now? |
Beta Was this translation helpful? Give feedback.
-
@alrz dotnet/roslyn#6182 does not have an LDM champion at the moment. |
Beta Was this translation helpful? Give feedback.
-
#377 apparently supersedes this. |
Beta Was this translation helpful? Give feedback.
-
As previously described here: dotnet/roslyn#6182
A prototype available here: https://github.com/orthoxerox/roslyn/tree/features/statement-expression
Statement expressions
Summary
Expressions can contain additional intermediate statements, including temporary local variable declarations.
Motivation
They work well with the ternary operator,
base
andthis
constructor calls and potentialmatch
expressions.They also can be lowered into (good for function pipes) (if the binder map can survive this, haven't investigated that yet).
Detailed design
See here: dotnet/roslyn#6182
At the moment I cannot access the markdown of the formal syntax of the proposed syntax, but it's an open paren, 1+ statements, an expression, and a close paren. Only declaration and expression statements are allowed. The value is the value of the expression.
Locals declared in the statements do not leak outside the statement expression.
Drawbacks
People will abuse it and turn their
?:
into an unreadable mess.Alternatives
Blocks and if statements could be changed to return values if their last statement is an expression statement or an if statement or a block that return a value.
Unresolved questions
What should happen to out vars and pattern variables declared in the expression? Are there any compelling use cases to make expression statements leak them?
Should we allow more statement types to be used inside statement expressions? Why?
Design meetings
None yet.
Beta Was this translation helpful? Give feedback.
All reactions