Skip to content

Railway Oriented Programming. Examples in Kotlin 1.3 with Result<T> data-type

License

Notifications You must be signed in to change notification settings

ChameleonTartu/railway-oriented-programming-presentation

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

13 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Railway Oriented Programming in Kotlin

Build Status

What is that?

Railway Oriented Programming (ROP) is a functional programming technique that allows sequential execution of functions, not necessarily synchronous. The key concept is that each function can only accept and return Container of either Success or Failure. Failure wraps Throwable type and Success can be of any type.

Visual representation of ROP

ROP simple

Therefore, in the simple scenario each function can go from Success to either Success or Failure scenario and Failure can go to Failure scenario.

In the real world, you may want to transform Failure to Success, not only to Failure. This procedure is called rescue or recovery; the name may wary from language to language.

Visual representation of ROP detailed is

ROP simple explained

It is important that you can transform from each state to each other state.

How to use in practice?

This concept is present but not promoted in Kotlin since Kotlin 1.3. The matter of fact Kotlin KEEP states that feature may change in the future. At the time of writing this blog post, Result<T> is a Container-like type which can be used inside generic containers such as Collection<T>.

fun capitalize(cities: List<String>): List<Result<String>>

In the meantime, Result<T> can be parameter of functions.

fun map(Result<String>): List<String>

so-icon 30x30

Result<T> data-type has docs .

How to apply to existing code?

Example of functions that can have this paradigm and how to write adapters to those functions:

All classes have corresponding tests, so you can compare difference in testing. Since adapters are primitive the more real-world example is into Application. The idea is to download HTML pages of URLs and recover in those cases when URLs are not valid.

Who are inspires?

This repo was inspired by:

Railway oriented programming: Error handling in functional languages by Scott Wlaschin

returns

Acknowledgements

Thank you for all inspires of this blog post. To my colleagues, especially to Thomas Borgen how showed my this technique in Python. Also, thank you Thorsten Heller who supported me and inspired to put in text. Also, to my first and patient listeners Cloudwheel team!

About

Railway Oriented Programming. Examples in Kotlin 1.3 with Result<T> data-type

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages