Proposal: Overridable default
operator
#167
Replies: 7 comments
-
What happens with |
Beta Was this translation helpful? Give feedback.
-
At the risk of sounding like a stuck record: that's why features such as this needs CLR support. Of course the killer counter to this is that the CLR team are unlikely to countenance the decrease in performance that such checking for an override would incur. So I may be left sounding like a stuck record until I finally shut up 😀 |
Beta Was this translation helpful? Give feedback.
-
How do you solve the problem of a valid "default" fraction? struct Fraction {
double numerator;
double denominator;
Fraction() { // error!
numerator = 0;
denominator = 1;
}
double ToDouble() => numerator / denominator;
bool IsValid => denominator != 0;
} As explicit parameterless constructors are not allowed for structs something like a I believe there are plenty of situations out there where the definition of a "default value" with appropriate operators would make much sense in respect to data coherance. |
Beta Was this translation helpful? Give feedback.
-
To continue sounding like a stuck record, just allowing parameterless constructors won't help for your |
Beta Was this translation helpful? Give feedback.
-
@DavidArno In my opinion, there is no sane default value for the numerator and denominator of a fraction. Far better would be forcing I think |
Beta Was this translation helpful? Give feedback.
-
Because it has not been mentioned here explicitly. This proposal is directly related to #99. |
Beta Was this translation helpful? Give feedback.
-
Update: because of the no-arg struct constructor discussion, I would not mind having Perhaps something like this: struct FractionWithArbitraryDefaults
{
public readonly double Numerator;
public readonly double Denominator;
public FractionWithArbitraryDefaults(double numerator, double denominator)
{
Numerator = numerator;
Denominator = denominator;
}
// Metadata that specifies pattern- *not* code that will be executed to create the pattern
default { Numerator(0), Denominator(1) }
} Constant (blittable) default initialization makes sense. Executing arbitrary code X times however does not make sense for
Default<int>.Value
Default<object>.Value
Default<MyStruct>.Value
static class Default<X>
{
public static readonly X Value;
} Never should that code result in executing user constructor code. That is what |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
As previously seen on dotnet/roslyn#71
Problems
When we get non-nullable reference types, what happens when we write
var os = new object[10]
instead ofvar os = new object?[10]
?When we get value types with parameterless constructors, symmetry between
default(T)
andnew T()
gets broken for them.Solution
We should be able to override operator
default
. Yes, this means thatdefault
will no longer be always emitted asldnull
orldloca - initobj - ldloc
and anynewarr
will be followed by a loop.However, with CLR support this could be optimized for cases where a single static instance is returned by the overridden operator.
Beta Was this translation helpful? Give feedback.
All reactions