5 ms·
var area = figure switch { Line _ => 0, Rectangle r => r.Width * r.Height, Circle c => c.Radius * 2.0 * Math.PI, _
by twoquestions 8y ago
var area = figure switch
{
Line _ => 0,
Rectangle r => r.Width * r.Height,
Circle c => c.Radius * 2.0 * Math.PI,
_ => throw new UnknownFigureException(figure)
};
So more and more of F# is making it's way to C#. Between this and better tuple literals, I'm very happy about this!
(I still need to get around to learning F# for real though, I really love it's brevity.)
- neilsimp1 8y agoI'm looking at this as well. I'm confused about the word `figure` here. It looks like it's a keyword for this new type of pattern matching switch. but the word `figure` itself seems unneeded.
- chusk3 8y ago`figure` here is just an instance of Shape that was previously in scope. You've got to match on an instance of an object in this syntax.
- contextfree 8y agoIt looks like "switch" is an infix operator in this context, so "figure" is just the variable/value on which the match operates.
- GordonS 8y agoI often find that my switch cases as one-liners, so this looks great!