4 ms·
Good luck checking for `None` equality and indent your code like 5 times... a sort of railway programming[0] avoids the awkward checks for values, making functi
by hackandtrip 5y ago
Good luck checking for `None` equality and indent your code like 5 times... a sort of railway programming[0] avoids the awkward checks for values, making functions work with higher order types.
0: https://www.greenbird.com/news/railway-oriented-programming-in-kotlin#:~:text=Railway%20Oriented%20Programming%20(ROP)%20is,can%20be%20of%20any%20type https://www.greenbird.com/news/railway-oriented-programming-....
- ReleaseCandidat 5y agoThe original Railway: https://fsharpforfunandprofit.com/rop/ https://fsharpforfunandprofit.com/rop/ And his 'Against Railway-Oriented Programming': https://fsharpforfunandprofit.com/posts/against-railway-oriented-programming/ https://fsharpforfunandprofit.com/posts/against-railway-orie...
- impoppy 5y agoHow is this different from match case ok case err or from if result.val?
- hackandtrip 5y agoWell, the most basic pro is to avoid indentation and have pure function which are clear and have less assumption to take into account; if I know that my pipes always return me a result such as the one I want, and the "bad path" is discarded automatically, code is way cleaner. Imagine a code without `if result is not None` basically!
- IshKebab 5y agoOk if a is None: raise Exception() if a.b is None: raise Exception() if a.b.c is None: raise Exception() One of the advantages of Python/Typescript style type hinting is that it avoids the mega-nested `if`s that you often have to resort to in Rust. In Rust the above would not work: if a.is_none() { bail!(); } if a.b. // nope! a is still Option<>. It may be possible to fix that when enum variants are distinct types but as it stands you often have to do lots of indenting.