The Railway Pattern

Learn about the railway pattern and its correlation with monads.

Background to the Railway pattern

Scott Wlaschin, a prominent promoter of functional programming and a vocal leader in the F# community, advanced the Railway pattern, a formula for error handling using controlled function composition. This pattern is a novelty only in name, for it’s nothing more than an elaborate error handling with sum types such as the Either monad.

When dealing with real-world problems, it’s common to deal with both success and failure scenarios. The Railway pattern encourages us to conceive these as two switches on the track. Furthermore, according to the rubric, both pathways should provide similar values to aid in composition. This requires two-switch functions as a prerequisite.

Get hands-on with 1200+ tech skills courses.