5 ms·
The biggest problem for me is the age old „null“. C# with monads would be great.
by ghosty141 6y ago
The biggest problem for me is the age old „null“.
C# with monads would be great.
- jayd16 6y agoYou can set references to default to non-nullable now with compiler flags or some such thing.
- orthoxerox 6y ago<Nullable>enable</Nullable> in your .csproj
- mdpopescu 6y agoSomething which I've used in the past (edited because I spotted some mistakes while re-reading the code [grin]): public readonly struct Option<T> { public static readonly Option<T> NONE = new Option<T>(); // public T Value { get; } public bool HasValue { get; } public Option(T value) { Value = value; HasValue = value is { }; } public Option<TR> Select<TR>(Func<T, TR> selector) => HasValue ? new Option<TR>(selector(Value)) : Option<TR>.NONE; public Option<TR> SelectMany<TR>(Func<T, Option<TR>> selector) => HasValue ? selector(Value) : Option<TR>.NONE; public T OrElse(T defValue = default) => HasValue ? Value : defValue; } You can of course add more utility functions besides these, eventually as extension methods, but it's a starting point.
- ryanjshaw 6y agoYou can also use a library like LanguageExt. In practice I'm torn between how neat and logical it looks, and wasting 30min figuring out I used the wrong match() on a mixed sync/async scenario after a method signature changed and that's why my database context is disposed of randomly in the middle of operations.
- franknine 6y agoHighly recommend Functional Programming in C#: https://www.manning.com/books/functional-programming-in-c-sharp https://www.manning.com/books/functional-programming-in-c-sh... If you want to learn how to do some functional in C#. If you just need some ready-made monads, language-ext is the way to go: https://github.com/louthy/language-ext https://github.com/louthy/language-ext We tried to bring some functional ideas into our Unity3D codebase with help of these resources, it's hard but doable.
- iso8859-1 6y agoLack of null and monads are kinda orthogonal. Yes, you can use monads to model "early return", but you can use them for so many other things. F# already offers computation expressions, which you can use for monads (but with no typing). But even Haskell provides no enforcement of the monad laws. Surely you must know about F#, so your comment makes little sense to me. If you want HKT's in F#, why not just say so?