You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Overall, great proposal.
Just a minor nitpcik: * seems a little out of place in language. I propose to simply use var instead by omitting variable name:
case Mult(var, Const(0)): return Const(0);
case Mult(Const(1), var x): return Simplify(x);
If we would allow to match by only type name and skip variable name, one could even perform simple type test cases like this:
bool IsInteger(Expr e)
{
switch(e)
{
case Const(int): return true;
case Const(double val): return IsInteger(val);
case Const(var): return false;
...
}
}
Proposal. This is a example of a simple syntax transition to a c# record from the code above. The record is an immutable class with just default public readonly fields. Records could use the expected immutable class implementation.
recordCartesian{doubleX;doubleY;}varc=newCartesian{X=1.0,Y=2.0};varok=c.X;// okc.X=3.0;// Error, a readonly field cannot be assigned to.
Another option could be (although I would prefer the above)
recordCartesian{doubleX;doubleY;}varc=newCartesian(X:1.0,Y:2.0);varok=c.X;// okc.X=3.0;// Error, a readonly field cannot be assigned to.
I may be reading the spec wrong, but it doesn't look like it's specifying the return type of operator is. Shouldn't it force it to be bool? The == and != operators lack such a constraint, but IMHO there is no valid use for non-bool-returning equality operators.
Were you considering to use _ character instead of * for wildcard pattern? Every language I can think of use this character for that.
I sometimes use this character as lambda argument to indicate that this parameter won't be used. I know that _ is a valid name for variables and classes and you can write something like this (_, other) => _.PropertyAccess but maybe there is a way to use this character and don't break existing code? For example previous code would compile (maybe with warning) and this one would fail (_, _) => _.PropertyAccess because _ would be treated as a wilcard character.
And when it comes to pattern matching we could disallow to name variable as _ (for examle this pattern Cartesian(var x, var _) won't compile) and allow _ to be wilcard character. It's a new feature so old code won't be broken.
I just thing that _ is so prevalent in other languages that it is worth considering to use it instead of *. For someone familiar with pattern matching from other language it would be instantaneously clear what the code do when they'll see C# code with pattern matching and with this character. I think similarity with other languages is nice to have.
The example shows auto-implementing of ToString, but the spec says only that Equals and GetHashCode will be implemented.
I think auto-implementing of the == and != operators is a no-brainer here. IEquatable I don't feel strongly about as I never see types that actually use it.
I like the record keyword. It seems weird that the mere existence of a primary constructor would initiate all this other magic behavior as well. Doing so may lead non-primary constructor classes to become second class citizens (as auto-implementation of GetHashCode, Equals, and ToString will do the right thing in 95% of cases and thus be highly desirable). IMO, the magic associated with records seems orthogonal to primary constructors.
I really hope the design committee looks at the with operator. Immutability/records needs this operator to be useful without a ton of extra boilerplate for copying immutable types. As others have mentioned previously, Roslyn code would also benefit greatly from a with operator.
The biggest value adds for the code I write every day are records (mostly as proposed) + with operator. The pattern matching stuff is just nice to have gravy.
@MgSamIEquatable<T> is often used implicitly, for example in a Dictionary<TKey,TValue> or a HashSet<T> with the default comparer. EqualityComparer<T>.Default uses IEquatable<T> if it's available, or falls back to Object.Equals if it doesn't, meaning a perf/memory hit for value types due to boxing.
We have not specified ToString for records, nor operator == and operator !=. It is an open question whether or not we will do anything for them.
I'm not sure if a default ToString will be useful beyond debugging output. Maybe a default DebuggerDisplayAttribute will be more useful?
I like the idea of public Student { ... } to add code to the primary constructor.
+1 to that idea
We have solved the Square:Rectangle problem. The solution appearing in the draft specification is not the one we are going to end up with, but this margin is too small to hold the details. Stay tuned for more details.
I'm excited. Current spec forces you to use only abstract rectangles, if I'm reading it correctly.
Please implement == & != for records. I can't really see a case where one would not want == implemented exactly the same as .Equals() as shown in the examples. Not implementing them will therefore force everyone to either manually define them for every record (sort of defeating one of the point of records, to cut down on boiler-plate, or to force exclusive use of.Equals when handling records.
The with expression is a useful syntactic-sugar addition. Considering @HaloFour's example, what will really happen without this is peoples will write var bar = foo.With(123);, dropping the optional X:. Much better to provide a language feature that guides the developer into writing clearer code: var bar = foo with { X = 123 };. This offers a massive improvement in readability for very little compiler cost.
In the examples, both a sealed and abstract class record are shown. But the spec shows class-modifiers as optional, suggesting class records can be declared that are neither. What would be the point of them and what would the resultant syntax look like?
what will really happen without this is peoples will write var bar = foo.With(123);
This can be mitigated by shipping an analyzer+codefix in the box. In a case like this, I'd rather MS provide one rather than every Roslyn analyzer author implementing their own.
@gafter I do like to see with syntactic sugar for the sake of CamelCase property names. For the same reason I don't think a dedicated syntax to name the parameters and properties differently would be deadly useful unless you want to follow the naming convention when you write named arguments in the constructor. That said, I think the ability to use object initializer syntax for record construction can provide this code style consistency, specially when you have default values for properties and want to omit some of them short of their order using named arguments (Note that with with this is always the case).
As for @HaloFour's suggestion, to not look like a property, how about something similar to C++ syntax?
publicsealedclassStudent(stringName,doubleGpa){// no parameters permitteddefaultprivate Student(){// additional logic, if any}// still you can define a ctor without parameterprivateStudent():this("Batman",-1){}publicstaticStudentCreate(){...}}
Although the primary constructor should be accessible publicly for deconstruction short of its accessibility.
Activity
ghord commentedon Feb 3, 2015
Overall, great proposal.
Just a minor nitpcik:
*seems a little out of place in language. I propose to simply usevarinstead by omitting variable name:If we would allow to match by only type name and skip variable name, one could even perform simple type test cases like this:
I think it would be more consistent behaviour.
ztone commentedon Feb 3, 2015
F# record (for reference)
This is similar valid C# syntax
Proposal. This is a example of a simple syntax transition to a c# record from the code above. The record is an immutable class with just default public readonly fields. Records could use the expected immutable class implementation.
Another option could be (although I would prefer the above)
Just my 2 cents
casperOne commentedon Feb 3, 2015
Why would
IEquatable<T>not be implemented? You're overridingEquals(object)already, soIEquatable<T>should be a no-brainer.SolalPirelli commentedon Feb 3, 2015
I may be reading the spec wrong, but it doesn't look like it's specifying the return type of
operator is. Shouldn't it force it to bebool? The==and!=operators lack such a constraint, but IMHO there is no valid use for non-bool-returning equality operators.mpawelski commentedon Feb 3, 2015
Were you considering to use
_character instead of*for wildcard pattern? Every language I can think of use this character for that.I sometimes use this character as lambda argument to indicate that this parameter won't be used. I know that
_is a valid name for variables and classes and you can write something like this(_, other) => _.PropertyAccessbut maybe there is a way to use this character and don't break existing code? For example previous code would compile (maybe with warning) and this one would fail(_, _) => _.PropertyAccessbecause_would be treated as a wilcard character.And when it comes to pattern matching we could disallow to name variable as
_(for examle this patternCartesian(var x, var _)won't compile) and allow_to be wilcard character. It's a new feature so old code won't be broken.I just thing that
_is so prevalent in other languages that it is worth considering to use it instead of*. For someone familiar with pattern matching from other language it would be instantaneously clear what the code do when they'll see C# code with pattern matching and with this character. I think similarity with other languages is nice to have.MgSam commentedon Feb 4, 2015
Great to see the updated proposal!
Some comments:
ToString, but the spec says only thatEqualsandGetHashCodewill be implemented.==and!=operators is a no-brainer here.IEquatableI don't feel strongly about as I never see types that actually use it.recordkeyword. It seems weird that the mere existence of a primary constructor would initiate all this other magic behavior as well. Doing so may lead non-primary constructor classes to become second class citizens (as auto-implementation ofGetHashCode,Equals, andToStringwill do the right thing in 95% of cases and thus be highly desirable). IMO, the magic associated with records seems orthogonal to primary constructors.withoperator. Immutability/records needs this operator to be useful without a ton of extra boilerplate for copying immutable types. As others have mentioned previously, Roslyn code would also benefit greatly from awithoperator.withoperator. The pattern matching stuff is just nice to have gravy.gafter commentedon Feb 4, 2015
These record types can easily support 'with' expressions like
which would generate
gafter commentedon Feb 4, 2015
The
==and!=operators would only be a good idea for record structs. It is too easy to get in trouble with them on reference types.MrJul commentedon Feb 4, 2015
@MgSam
IEquatable<T>is often used implicitly, for example in aDictionary<TKey,TValue>or aHashSet<T>with the default comparer.EqualityComparer<T>.DefaultusesIEquatable<T>if it's available, or falls back toObject.Equalsif it doesn't, meaning a perf/memory hit for value types due to boxing.251 remaining items
HaloFour commentedon Mar 25, 2016
@gordanr
So spit-balling with #119:
Or with #8181:
In either case the contract could be applied to both the constructor and the generated
Withmethod.gordanr commentedon Mar 25, 2016
Or even simpler with help of non-nullable reference types.
That would be really awesome. Meanwhile we can only use explicit constructor.
HaloFour commentedon Mar 25, 2016
@gordanr Nod, the enforcement maybe as a last resort, just in case someone squeaks a
nullthrough anyway.orthoxerox commentedon Mar 25, 2016
Looks like the spec is missing the changes to the
class-basepart of the declaration:In the current c# spec,
class-baseis:orthoxerox commentedon Mar 25, 2016
@gafter
I'm not sure if a default
ToStringwill be useful beyond debugging output. Maybe a defaultDebuggerDisplayAttributewill be more useful?+1 to that idea
I'm excited. Current spec forces you to use only abstract rectangles, if I'm reading it correctly.
DavidArno commentedon Mar 25, 2016
Regarding the open questions/undecided stuff:
Please implement
==&!=for records. I can't really see a case where one would not want==implemented exactly the same as.Equals()as shown in the examples. Not implementing them will therefore force everyone to either manually define them for every record (sort of defeating one of the point of records, to cut down on boiler-plate, or to force exclusive use of.Equalswhen handling records.The with expression is a useful syntactic-sugar addition. Considering @HaloFour's example, what will really happen without this is peoples will write
var bar = foo.With(123);, dropping the optionalX:. Much better to provide a language feature that guides the developer into writing clearer code:var bar = foo with { X = 123 };. This offers a massive improvement in readability for very little compiler cost.DavidArno commentedon Mar 25, 2016
In the examples, both a
sealedandabstractclass record are shown. But the spec showsclass-modifiersas optional, suggesting class records can be declared that are neither. What would be the point of them and what would the resultant syntax look like?Joe4evr commentedon Mar 26, 2016
This can be mitigated by shipping an analyzer+codefix in the box. In a case like this, I'd rather MS provide one rather than every Roslyn analyzer author implementing their own.
alrz commentedon Mar 26, 2016
@gafter I do like to see
withsyntactic sugar for the sake ofCamelCaseproperty names. For the same reason I don't think a dedicated syntax to name the parameters and properties differently would be deadly useful unless you want to follow the naming convention when you write named arguments in the constructor. That said, I think the ability to use object initializer syntax for record construction can provide this code style consistency, specially when you have default values for properties and want to omit some of them short of their order using named arguments (Note that withwiththis is always the case).As for @HaloFour's suggestion, to not look like a property, how about something similar to C++ syntax?
Although the primary constructor should be accessible publicly for deconstruction short of its accessibility.
gafter commentedon Mar 28, 2016
The specs for pattern matching and records have been moved to https://github.com/dotnet/roslyn/blob/features/patterns/docs/features/patterns.md and https://github.com/dotnet/roslyn/blob/features/records/docs/features/records.md
There are new discussion threads at #10153 for pattern-matching and #10154 for records.
Fix Editor config issues (#206)