5 ms·
You can make the C# code much more F# like with records and a switch expression. Eg: https://sharplab.io/#v2:EYLgZgpghgLgrgJwgZwLQAUEEsC2UECeAwgPYB2yMCcAxjCQs
by tablloyd 3y ago
You can make the C# code much more F# like with records and a switch expression.
Eg:
https://sharplab.io/#v2:EYLgZgpghgLgrgJwgZwLQAUEEsC2UECeAwgPYB2yMCcAxjCQsgDQAmIA1AD4ACADAATcAjABYA3AFgAUH0FCAdABksZAI6Sp07gGZ+SGgxb9MJFrRgAKYQIByUHBCb8WEGrigAbY9hoQAlBo6eq6G/ADyCC4IymQQFiZmdN6m5k4qMPwAinBQZDBYMAQBWroQZHA4/AAiWMgGcHkAKgQADhD8AN7GEAi+eVAA5o78AGJYAB4QLACCOCQNGQC+JcEGkdW19XkWNXXzTa3tzW1OLm54XgBqnnD+gbr6oRFRFsqUADzPPTEQAHzhkW+KhQTl2WwyYP2MD80g60n4CMEujO7i8RE8NDgHlgEEaJBgnkwWF8/AAvL94YiqV9osDkPIAMoVCwkLzk/is+QJcxcnztABUHI88myuXyhT8/GQAHcCjQABaUqkIuFSZXq/gAN3w/DavTKBKG/Gl8rKGz2C34tU6/GOEBA5vBdq5PT6hvaizJ/z1bsGAsdUPk1w8t34AHp+EJeLwALJMJUahHahD8MATKazKHG01kAOW61dO0OyELZ1jSYzOaWz3stMVzMLeNqxOI5P8MgkEt5L3tzubKEJqmLDSLIA=== https://sharplab.io/#v2:EYLgZgpghgLgrgJwgZwLQAUEEsC2UECeAwgP...
- sbelskie 3y agoFWIW, this a switch expression rather than a switch statement. But in any case I really love this addition to the language but the inability to have multi-line or block expression arms is a constant annoyance for me. You can even combine these with the new one line record syntax to create a poor man’s discriminated union.
- ComputerGuru 3y agoYour pedantic correction is actually important for another reason: the switch expression (unlike the switch statement) is defined at the language level as an expression (evaluating to/“returning” a value), which would be ok except C# doesn’t (at the language level) have a void/unit type, meaning the switch statement has to return an actual value, limiting the places you can use it compared to the F# counterpart (or the match expression from rust, etc) to very specific cases, usually those performing an assignment. The workaround for that is the same as the workaround for the really lame one line limitation: you need to call a (preferably (static) local) function in the handler portion and then return something like `true` assigned to a discard. Hacks all around! Eg _ = foo switch a when … => CaseA(foo), _ => CaseB(foo); With CaseA and CaseB returning bool in order to call a function depending on the value of foo rather than assign a value.
- jcpst 3y agoMicrosoft has a good tutorial that showcases what you can do when designing with switch expressions in mind: https://learn.microsoft.com/en-us/dotnet/csharp/fundamentals/tutorials/pattern-matching https://learn.microsoft.com/en-us/dotnet/csharp/fundamentals... Probably one of my favorite recent-ish additions to the language.
- rjbwork 3y agoThe fact that they've added all of this great stuff and have STILL not added real discriminated unions is a damn travesty. It would just so drastically improve the language.
- isanjay 3y agoWould love to see DU in C# I think they would add it by C# 14 or 15.
- pjmlp 3y agoMaybe the rest of the .NET world has more important stuff to care about? Anyone that cares so deeply about them can do the work on a F# assembly.
- Risord 3y agoYea I don’t care neither but business seems to care. If you listen closely their specs are pretty much bloated with DUs.
- diarrhea 3y agoI’m not arguing there is more important stuff, but DUs would enhance the language on a wide, fundamental level, accelerating a lot of other advancements.
- pjmlp 3y agoI have used Caml Light, Standard ML, Objective Caml, Haskell, and fail to see the greatness of such advancements. C# has done great in the industry for the past 23 years without them.
- oaiey 3y agoThe blog is indeed the worst example you could run for this case. As you pointed out, modern C# would be same length (which is his "wow" effect on the functional core) but also has the F# not really a higher readability than the shown C# code. F# / C# interop ... cool thing, just to clarify.