Skip to content

Comment on Monads, or Programmable Semicolons (2014)parent

Comments

One of my favorite secrets about C# is that query syntax is actually a form of do notation. You can create your own interfaces that conform to the monad laws, and the compiler will happily let you apply query syntax to them the same way you can to IEnumerable.

That said, it's not something to actually make use of if you're trying to make your code readable, since the keywords are so tightly coupled to the LINQ use case.

I had to do a project in C# many years ago, small piece of code that needed lots of concurrency and error handling. We were able to write it in a very clean monadic DSL (ala do notation in haskell or 'for' in Scala) by wrapping Task in lawful monad types, we wrote monad transformers, etc. We ended up being extremely productive and getting good performance.

I would almost never recommned doing something like this given the maintenance issues and how long it takes to bring the average C# programmer up to speed, but this was three fairly advanced FP folks working on the application. It was probably some of the cleanest C# I've gotten to work with.

AboutSource Built by g1lg1l

Hackerly is an independent reader for Hacker News, built on the public HN API. Not affiliated with Y Combinator.