Pro tip: don't write F# like you would write C# - then you might as well write C#. Take the time to learn the functional primitives.
Pro tip: don't write F# like you would write C# - then you might as well write C#. Take the time to learn the functional primitives.
let bar =
if foo then
7
else
11
or let bar =
try
// code that might throw
7
with ex ->
11
and will ensure that both/all code branches return a compatible type for the `let` binding.Whereas in C# you have to do like
int bar;
if (foo) {
bar = 7;
} else {
bar = 11;
}
And C# will let you know if you omit the `else` on accident ...Except that most C# developers do
int bar = 0; // or some other default value`
to get the red squiggly to go away while they type the rest of the code, unknowingly subverting the compiler's safety check.This doesn't seem like a big deal given these examples. But it becomes a much bigger deal when the if/else grows super large, becomes nested, etc.
bar = foo switch
{
true => 7,
false => 11
}1. It doesn’t support code blocks, so if you need multiple lines or statements you have to define a function elsewhere. 2. To get exhaustiveness checking on int-backed enums you have to fiddle with compiler preprocessor directives.
And for #2 any data associated with each enum variant is left implied by C# and has to be inferred from a reading of the surrounding imperative code, whereas in F# the union data structure makes the relationship explicit, and verifiable by the compiler.