I think switch could have been made into an expression very easily by following the lead of lambdas and method-body expressions, namely the => arrow.
var area = switch
(
case Line l => 0;
case Rectangle r => r.Width * r.Height;
case Circle c => Math.PI * c.Radius * c.Radius;
default: throw new ApplicationException();
)
That 'feels' to me to be consistent with the expression approach taken elsewhere, whilst making it clear to anybody used to seeing the statement based switch that it's different.
> so the pattern matching overall is way less useful to me without records/discriminated unions.
I agree partially. I think it's possible (I'll investigate tomorrow) to provide an 'is' operator override. So for example in my implementation of Option<T> [1] (which would usually be a sum-type of Some(x) and None, I instead use a struct (to remove a 3rd state of null).
With the 'is' operator I'm hoping to do this:
public static class Some
{
public static bool operator is(Option<T> option, out T value)
{
value = option.Value; // Value is internal
return option.IsSome;
}
}
switch(option)
{
case Some x: ...; break;
default: ...; break;
}
I'm not sure if I can do this:
public static class None
{
public static bool operator is(Option<T> option)
{
return option.IsNone;
}
}
switch(option)
{
case Some x: ...; break;
case None: ...; break;
}
So although it doesn't solve the general need for sum-types and record-types, it should hopefully facilitate some of the common use cases.
Agreed about ref and out; I understand their purpose, I just despise them with a passion. Their whole syntax and ickyness makes me feel queesy. The inline var out feels like an imbalanced expression. They're clearly more useful now, but they still feel odd.
[1] https://github.com/louthy/language-ext/blob/master/LanguageE...