-
Notifications
You must be signed in to change notification settings - Fork 4k
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
Discussion: let statement #9876
Comments
I don't like the idea of introducing local shadowing, using any syntax. I don't see why You aren't really talking about multiple variables, you're talking about multiple patterns. From what I understand let (Point(var x, 0), Point(0, var y)) = (point1, point2) else throw new Exception(); As for the final point, I don't see much benefit for having the alternate |
Because it might introduce new variables in the pattern, and they potentially make the target useless. For example in
Even if the intermediate tuple optimizes away, the readability is far from my example's. It gets worse when you want to use
There is no new syntax there, just a relaxed rule, because patterns are meant to check for null anyway. Actually it might be confusing that
There is more. |
I understand what you're trying to do, that's generally why anyone would want to shadow. But I think it's a slippery slope and I don't think that it's too onerous to have to assign the pattern results to new identifiers even if you don't plan on using the old one again within that scope.
I think it's more explicit about what is actually happening, though, whereas you're looking for special syntax to handle multiple patterns within a single deconstruction.
Variable patterns aren't. That's even true in F#. If you want the null check you should use the type pattern.
Pretty sure that wasn't there when I was typing my comment. 😄 I'm pretty sure that requiring Note that I'm largely playing Devil's advocate about a lot of this. I don't have that strong of an opinion one way or another. |
As it turns out, no. For example, in #8016 it is proposed that one should be able to shadow locals just for the sake of it and no other reason, really. Since, as I said, it would be not possible to do that and you can just use the target name (if it was an identifier and nothing else), nothing can possibly go wrong.
It's more explicit but it's like you're inlining everything. In this specific example, you don't want to use a single
Yes, I meant in case of a type pattern which you would normally use
Yes that is, because
Does this make any sense? let Point(x, y) = point;
That is like saying typos in |
I've updated the openning post to mention |
Please expand
which is semantically the same as:
This is compatible with expression 'bodies' too. Obviously the compiler should optimize the closure and call out, but keep scoping intact. Another alternative would be:
|
It sounds like you want some kind of new syntax to allow for inline declaration and invocation of a local function. I don't see what that would have to do with |
Ah, if the point is to describe a sequence of operations that are to be treated as an expression, then yes, #6182 appears to be the right feature. And it should work with |
@alrz Can you make it clear this is primarily used for deconstruction (somewhere near the top)? :) |
Obsolete. |
Since #6400 was closed, I wanted to discuss some other aspects of
let
statement and potential improvements.Variable shadowing
A relaxed form of shadowing were discussed at #8016 which didn't seem to be a desired behavior. I want to propose a restricted form of shadowing for
let
that allows to use same variable name in the pattern as the target,This allows us to use the same name wherever it makes sense, e.g.
Multiple variables and patterns
Since #4294 is closed, I think it'd be nice to be able to declare multiple variables with
let
,For example,
It helps to use a single
else
for all fallible patterns,which prevents code duplication.
Null check
As it is specified, the simplified form of
let
statement always succeeds, which would makeelse
part unnecessary. I want to suggest to allowelse
for all nullable expressions, so,As an alternative to the followings,
which introduce a level of additional indention that
let
is trying to avoid in the first place.Identifiers as variables
Currently
var
is being used to disambiguate identifiers and new variables in patterns,Considering that this will be very common I want to suggest that
let
turns every identifier to a new variables in patterns, for example.The syntax can be used in
switch
statements (Swift-like),Same analogy applies to
let
statement andcase
expression,As patterns
We can use
let
as as-pattern instead of an identifier after pattern, e.g.So I propose replace
var
patterns with this production rule:let expressions
It would be nice to allow decomposition assignment for complete patterns as an expression (#254).
One use case is in
using
statements so that one be able to write this:In this example,
let
returns aFoo
which isIDisposable
and variablex
is scoped insideusing
.The text was updated successfully, but these errors were encountered: