fut icon indicating copy to clipboard operation
fut copied to clipboard

Recursive pattern in NET8

Open andr1972 opened this issue 2 years ago • 1 comments

In file CiREsolver.cs:985 is

CiMethod method = symbol.Symbol switch {
	CiMethod m => m,
	CiMethodGroup group => group.Methods.FirstOrDefault(m => CanCall(m, arguments))
		?? /* pick first for the error message */ group.Methods[0],
	_ => throw StatementException(symbol, "Expected a method")
};

I am trying compile if with mono in Linux in version NET 7.3 without this "recursive pattern" feature. How can I downgrade this piece of code? Here is for my odd switch - value symbol.Symbol is not compared to known values but to definitions different types (?)

In usual switch form this will?

CiMethod method;
switch (symbol.Symbol)  {
	case (CiMethod m): //<-------------here error
		method = m;
		break;
	case (CiMethodGroup group): //<-------------here error
		method = group.Methods.FirstOrDefault(m => CanCall(m, arguments))
				       ?? /* pick first for the error message */ group.Methods[0];
		break;
	default: throw StatementException(symbol, "Expected a method");
};

Finally, this will (?)

CiMethod method;
if (symbol.Symbol is CiMethod)
	method = (CiMethod) symbol.Symbol;
else if (symbol.Symbol is CiMethodGroup)
	method = (symbol.Symbol as CiMethodGroup).Methods.FirstOrDefault(m => CanCall(m, arguments))
			       ?? /* pick first for the error message */ (symbol.Symbol as CiMethodGroup).Methods[0];
else throw StatementException(symbol, "Expected a method");

andr1972 avatar Mar 12 '22 20:03 andr1972

Last time I checked Mono it reported a NotImplementedException. Why bother with Mono if you can have official .NET on Linux?

The syntax you are asking about is a switch expression. You can replace it with a pattern-matching switch statement - the way you wrote it, but without the parentheses after case.

Your last example should also work. You can even consider using the ternary operator cond ? a : b.

pfusik avatar Mar 13 '22 07:03 pfusik