Does an IO monad make sense in a language like C#

后端 未结 6 2033
一生所求
一生所求 2021-01-01 17:38

After spending a lot of time reading and thinking, I think I have finally grasped what monads are, how they work, and what they\'re useful for. My main goal was to figure ou

6条回答
  •  礼貌的吻别
    2021-01-01 18:08

    You ask "Do we need an IO monad in C#?" but you should ask instead "Do we need a way to reliably obtain purity and immutability in C#?".

    The key benefit would be controlling side-effects. Whether you do that using monads or some other mechanism doesn't matter. For example, C# could allow you to mark methods as pure and classes as immutable. That would go a great way towards taming side-effects.

    In such a hypothetical version of C# you'd try to make 90% of the computation pure, and have unrestricted, eager IO and side-effects in the remaining 10%. In such a world I do not see so much of a need for absolute purity and an IO monad.

    Note, that by just mechanically converting side-effecting code to a monadic style you gain nothing. The code does not improve in quality at all. You improve the code quality by being 90% pure, and concentrating the IO into small, easily reviewable places.

提交回复
热议问题