4 ms·
good thing c# supports pattern match too. just this morning, I wrote something like this var thing = (enumA, enumB) switch { (EnumA.A,EnumB.A)
by lemper 3y ago
good thing c# supports pattern match too. just this morning, I wrote something like this
var thing = (enumA, enumB) switch
{
(EnumA.A,EnumB.A) => DoStuff(),
_ => throw new Exception(),
}
suffice to say, c# is pretty ok, coming from ml family.
- KineticLensman 3y agoRust's match is pretty cool as well. Here's Fizzbuzz, lifted from [0] match (i % 3 == 0, i % 5 == 0) { (false, false) => { }, (true, true) => { FizzBuzz }, (true, false) => { Fizz }, (false, true) => { Buzz } } I'm moving from C# to Rust at the moment and really enjoying it, especially now that I (almost) understand the borrow checker [0] https://stackoverflow.com/questions/43896431/c-sharp-equivalent-of-rusts-match https://stackoverflow.com/questions/43896431/c-sharp-equival...
- recursive 3y agoCan you match to (i % 3, i % 5) instead?
- Philpax 3y agoYou can, yes: https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=0c8baf63af6af6fed10a62b9d0e9a6dc https://play.rust-lang.org/?version=stable&mode=debug&editio...
- jsmith45 3y agoAs you hopefully know, the modern C# equivalent is not bad either, especially with top level statements and implicit global usings (The following is the whole program): foreach (var i in Enumerable.Range(1,100)) { Console.WriteLine( (i%3==0,i%5==0) switch { (true, true) => "FizzBuzz", (true, _) => "Fizz", (_, true) => "Buzz", (_, _) => $"{i}" } ); } This is so much nicer than what was possible back when the linked question was asked.
- recursive 3y agoIt gets a bit nicer still. (i%3,i%5) switch { (0, 0) => "FizzBuzz", (0, _) => "Fizz", (_, 0) => "Buzz", (_, _) => i }
- Smaug123 3y agoEnums are a bit leaky, though, because C# enums can just have any old `int` chucked into them, so you really do need the final arm of the `match`. `(EnumA)100` will compile just fine!