This is the third part of the series Functional error handling in F# and C#. In this post we will see how the command line argument parser with functional error handling, that was shown here using F#, can be implemented in C#.
Month / September 2015
Functional error handling – parsing command line arguments in F#
In this post, which is the second part of the series Functional error handling in F# and C#, we will examine an application for parsing command line arguments with error handling.
The functional approach uses Applicative Functors. Applicative Functors and the basics of functional error handling, only with immutable objects and without the use of exceptions, are explained int the first post of this series: Error handling with Applicative Functors in F# and C#.
A complete solution with all the code samples from this series can be found here.
As a starting point I took this F# console application template and reimplemented it with railway oriented error handling in F# and C#. The original version writes error messages directly to the console, which is totally ok in many situations. However, the railway oriented approach offers a bit more flexibility and safety because it will let the caller decide on how to handle the failure cases.
Error handling with Applicative Functors in F# and C#
In an object-oriented context a typical way to do error handling is to use the Notification Pattern described by Martin Fowler.
In functional programming (FP) the approach to error handling is very different. Error handling has to be done without mutating state and without the use of exceptions. The concept that is capable of this, and that is commonly used in FP is called "Applicative Functor".
In this post, which is the first part of the series Functional error handling in F# and C#, we will cover all the basics that are needed to do functional error handling in F# and C#, and to understand Applicative Functors.
The F# and C# code samples from this post can be found here.
In the second part of the series we will look at a sample application for parsing command line arguments with error handling in F#.
Anyway, before we jump right into it, we will need some introduction.
Series: Functional error handling in F# and C#
Some time ago when I started learning functional programming I also started to follow functional programmers on twitter. Some day someone stated that error handling should be done with Applicative Functors.
No, Martin Fauler, you should use applicative functors instead http://t.co/kLCJvLazqE
— vaskir (@kot_2010) December 9, 2014
Off course the Fowler way is a very typical, commonly used and approved approach. So what makes it so much better to use Applicative Functors instead? And what are they anyway?
This series of posts will answer these questions. It is the kind of article I was looking for back then but could not find. It is a compilation of all the pieces and resources that I found researching the topic.
In the first part we will cover all the basics that are needed to do error handling in a functional style, and to understand what is going on behind the scenes. We will see how error handling can be done only with immutable objects and without using exceptions.
In the second part of the series we will look at a sample application for parsing command line arguments with error handling in F#.
Content of this series:
-
Error handling with Applicative Functors in F# and C#
Basics of functional error handling -
Functional error handling - parsing command line arguments in F#
Functional error handling in practice (F#) -
Functional error handling – parsing command line arguments in C#
Functional error handling in practice (C#) -
Functional vs. imperative error handling
A comparison of the functional vs. the imperative approach to error handling