Proposal: Operators as method groups. #598
Replies: 7 comments
-
I think being able to say "Type.new" to be able to reference a constructor as a method group would also be nice. |
Beta Was this translation helpful? Give feedback.
-
@CyrusNajmabadi What about argumented ctors? Possible syntax: |
Beta Was this translation helpful? Give feedback.
-
Type.ctor is already a legal expression. Type.new is not. :)
We don't have any special syntax to pick a particular overload of a method group. So i don't htink we'd have anything for constructors. |
Beta Was this translation helpful? Give feedback.
-
Both |
Beta Was this translation helpful? Give feedback.
-
Is it possible to make operators to being working with null propagation (as method calls, for example), e..g:
which is the equivalent of:
|
Beta Was this translation helpful? Give feedback.
-
Operators already work on Nullable value types, last I read. |
Beta Was this translation helpful? Give feedback.
-
You're right. Then it's just private int Test(int? value) => (value + 5) ?? 0; However, it's a compiler magic. It wont work for the custom code private int Test(MyClass value) => (value + 5) ?? 0;
class MyClass
{
public int Value { get; }
public MyClass(int value) => Value = value;
public static MyClass operator +(MyClass a, int value) => new MyClass(a.Value + value);
} |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
It would be quite convenient if standalone operators were treated as method groups. Instead of
xs.Aggregate((x1, x2) => x1 + x2)
you would writexs.Aggregate(+)
. If that's potentially syntactically ambiguous, the operator symbol could be wrapped in parentheses or prefixed with@
or another symbol.Beta Was this translation helpful? Give feedback.
All reactions