-
Notifications
You must be signed in to change notification settings - Fork 1k
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
Proposal: as-based declaration expressions #402
Comments
Why is this an improvement? The only benefit I see is that it lets you cram more onto a single line expression. But I find the "current syntax" version to be significantly easier to read. |
It's an improvement in the same way that var pointMass1 = new PointMass(mass: 1.0f);
var pointMass2 = new PointMass(mass: 2.0f);
var simulation = new Simulation
{
pointMass1,
pointMass2,
new Spring(pointMass1, pointMass2)
}; versus var simulation = new Simulation
{
new PointMass(mass: 1.0f) as var pointMass1,
new PointMass(mass: 1.0f) as var pointMass2,
new Spring(pointMass1, pointMass2)
}; |
This is less a pattern unto itself and more the use of patterns with the SomeObjectReturningFunction() as string text; Is SomeObjectReturningFunction() as int number; What about here? Should that even be legal or should And what about: SomeObjectReturningFunction() as Point(var x, var y); If the function doesn't return a And in all of these cases is the type of the expression still I'd assume that these pattern variables would have the same scoping rules as |
@HaloFour I think that the least confusing approach, as long as the new operator is called
|
@HaloFour SomeObjectReturningFunction() as string text; // text will be null if the returned value is
// not a string. text is type string
SomeObjectReturningFunction() as int number; // won't compile (same as
// existing "as")
SomeObjectReturningFunction() as int? number; // would work fine
SomeIntReturningFunction() as int number; // would work fine
struct ValuePoint ... // defining double X and Y with Deconstruct
class ReferencePoint ... // defining double X and Y with Deconstruct
SomeObjectReturningFunction() as ValuePoint(var x, var y); // won't compile (same as
// existing "as")
SomeValuePointReturningFunction() as ValuePoint(var x, var y); // works fine
SomeObjectReturningFunction() as ReferencePoint(var x, var y); // either won't compile, or
// x & y will be double? and
// null if result isn't
// ReferencePoint
I think it would have to, unfortunately. |
I do not want to be a grouch, but I can't see any income from this pattern except worse readability of code. |
Out of curiosity, do you have the same view for the |
To be honest,
But on the other side I have to say I new switch syntax - it helps readability of code. |
Then it is a case of very different ways of coding (and views on what's readable). In my view, the if (x is T y) y.DoSomething();
|
Since I saw this issue and started paying attention, I've wanted this feature about twice a day while coding. It feels natural beside |
@DavidArno ,
I think the ugly part is that you have outer-scope declaration in nested statement. If it looks like:
it would be clear and easy to read, but it isn't:) So I have to say, that your |
I think there is some confusion in this proposal regarding "as" patterns, syntactically it would be defined under "pattern" rules: pattern: as-pattern: This is how F# as-patterns are defined. So, that is not to say that we extend the existing Furthermore, if (o is Identifier as Identifier) And by the way, this exists in current spec draft: the identifier following a complex pattern: if (x is (1, int a) t)
if (x is P(int a) p) Usage of |
EDIT - I have removed references to patterns as calling this an "as pattern" doesn't help with conveying its purpose.
With C# 7, we now have a new
x is var y
construct, that allows a new variable to be introduced within an expression, for later use in that expression:This works great in situations where a
bool
result is required from the expression. However, sometimes the result of the exression needs to be the variable's type, notbool
. With reference to Declarations within object initialisers, this could be achieved by allowing assignment statements to be used in expressions, such as in the following example:However, as alternative would be to copy the style of
x is T y
expression, by introducing a newx as T y
expression:This could then have the same scoping rules as
is
and work in a near-identical fashion, save that the result of is of typeT
, rather thanbool
.Update 18th May 2017
@mattwar's alternative proposal for variable declarations in expressions has highlighted more use-cases for using
as
in this way.Rather than having to declare a variable in advance and then use it in multiple tests in an
if
orwhile
:x as var y
could be used to introduce a variable in the first test, allowing it's re-use in subsequent ones:Also, when combined with recursive pattern matching, it could be used create a pattern that allows both testing of the innards of a tuple as well as introducing a variable of that tuple type:
The text was updated successfully, but these errors were encountered: