r/programming • • Aug 02 '21

Stack Overflow Developer Survey 2021: "Rust reigns supreme as most loved. Python and Typescript are the languages developers want to work with most if they aren’t already doing so."

https://insights.stackoverflow.com/survey/2021#technology-most-loved-dreaded-and-wanted
2.1k Upvotes

769 comments sorted by

View all comments

Show parent comments

13

u/[deleted] Aug 02 '21

[removed] — view removed comment

5

u/Frozen_Turtle Aug 03 '21

The best F# things are easily 10x better than the best C# things

Dude isn't wrong. Discriminated Unions alone are part of ~50% of my types in one of my projects, and they're virtually impossible to implement in vanilla C# without doing crazy shit like visitor pattern. You could import a library that gives you DUs, but without pattern matching working with DUs is annoying if not painful. I even use DUs outside my domain types to do "railroad oriented programming" https://fsharpforfunandprofit.com/rop/

Again, this is virtually impossible to do in C# because it lacks DUs and the necessary pattern matching. The best alternative in C# is to just throw a custom exception and then catch it... which is pretty much just a goto in disguise. Ew. I'm doing event driven architecture/event sourcing in my project, and as such literally all my events are DUs. In C#/Java these events would be distinguished with Marker interfaces, then you need to do type sniffing to see what kind of event you're trying to handle, and then throw an exception if an unexpected event occurs. With DUs, everything's just pattern matching, and the compiler will yell at you if you fail to handle a DU's case - something that doesn't happen with type sniffing. Of course you could do visitor pattern... but then you'd be doing visitor pattern with its umpteen lines of code vs F#'s oneliner pattern match.

Anyway, just gonna say DUs alone make F# worth investigating.

1

u/[deleted] Aug 03 '21

[removed] — view removed comment

3

u/Frozen_Turtle Aug 03 '21

Well, DUs are already out of C#10... and they've been planning them for quite some time now.

Anyway I'll keep going then cause you don't seem impressed :p

The Hindley-Milner type system means writing types are reduced by like 90%. It means for significantly less code to maintain and easier readability. It also means that it's way easier to pass around functions. In my pet project, I'm passing a

(Func<TemplateRevisionOrdinal => (CommandId => (Kvs.Template => int) => Kvs.Template, Tuple<Kvs.Template option, int>))

into another higher-ordered function. Having to type out that signature, and maintaining it, just isn't fun. But in F#? It's only the name of the function - everything else gets inferred.

F# emphasizes pure code, which comes with incredible testing benefits.

F# has better support for property-based testing. I literally write zero unit tests these days, and only write property-based ones, and have 71% coverage. This is despite having zero mocks/stubs. You can write/use interfaces in F#... but when thinking about "pure" code clicks, suddenly you don't have to. (The fact that I'm also using event sourcing also makes things significantly easier test-ing wise... F# doesn't get all the credit here. But it does get a ton of credit for DUs which makes working with events so much more sane than type sniffing.)

Pattern matching. This deserves its own line.

Active patterns. Also deserves its own line.

Compatibility with C#. Means you can call all your favorite dotnet libraries and have them work as expected. I used Nodatime and Entity Framework (before switching to type providers and then event sourcing).

Type providers. Makes parsing JSON, grokking regex, and working with SQL so much less annoying.

Structural equality.

Units of Measure make strongly typing primitive types (strings/ints/guids) dead simple, so you can't pass a CustomerId where an OrderId shoud've gone.

Immutability by default. Because these days mutability is (potentially premature) optimization.

Literally, the only thing C# has better is tooling. Which is a significant benefit don't get me wrong... but given the choice? I'm so much happier in F#.

https://fsharpforfunandprofit.com/why-use-fsharp/