Domain modelling with the F# type system: Make illegal states unrepresentable

22-05-2015

A review of the Devnology F# Meetup with Scott Wlaschin by Aage ten Hengel

Monday’s meetup focusing on domain modeling with F# was hosted by Infi, at their offices located near the city center of Utrecht. Their kind hospitality, the free pizza and F# stickers got things off to a good start.

Around seven o’clock everybody made their way to the beautiful, old-fashioned lecture hall where Scott would have his talk. After a brief introduction on who the Infi-Team is, Scott began the topic at hand: Domain modeling with F#.

Scott addressed the fundamentals of F# (and functional programming) fast pacing through a couple of slides, but giving room for questions at all times. Scott comes across as a kind, passionate and funny man (“for those who don’t know what a service is, it’s just a microservice without the ‘micro’ in front of it” – addressing the current microservice fad). He has a clear and visual way of explaining what makes functional programming unique.

Functional programming focusses on a few central building blocks: Functions (well, yeah), Composition (chaining functions makes a service, chaining services makes a use-case) and Types, which is the topic of this meetup.

One of Scott’s main points is that Types help you express your domain model in a very concise and explicit way. He has an example of an entire domain model (with about 10 types) on a single slide, as fully functional code. A key point he makes is that you can (and maybe should) type everything; a RegisteredCustomer is something else than a GuestCustomer and this should not be determined by setting (or forgetting to set) a flag on a Customer.

The other main point is that F# allows you to type your domain model in a way so that you could literally could not write incorrect code. He shows a type Payment type which has an Amount field of type PaymentAmount, where the PaymentAmount is of type decimal. At first I thought you could just make the Amount of type decimal, but the way Scott types this model makes sure you can’t accidentally set this field with a value of another type of type decimal. This ability eradicates the necessity of some unit tests and guarantees compile-time correct code.

This was a great talk, despite the 5 min. one-on-one Scott has with one of the audience members, much to the annoyance of some others (for which he apologized afterwards). I’m very much intrigued by F# and functional programming and looking forward to see whether the promises it makes (Conciseness, Rapid development, High Quality software and Fun!) hold up and if I can wrap my brain around it. Some of the tips Scott had I’m even taking back to my cozy OO world.

News @devnologynl

There you have it: Devnology predicts 2027. Thanks all for sticking with us over the years! https://t.co/pMmr5PCztx

19-04-2017 at 20:01

Nerds talking the last 8 years, and making predictions for the next. https://t.co/VGT8XIFQ0j

19-04-2017 at 18:37

@_angelos opening the last Devnology meeting with a tweet of the first one he attended https://t.co/ufAYScx3cn

19-04-2017 at 17:08

Qwan 5dff39510bacfcefb54e89f953eddfc1a7a21185b7128d96ff6b466f56acb6d9
Macaw 06e9331a5321067b592bf45ea39db7df6792dc976000d24d3ee4043d99203514
Finalist e304343cdbeb0996cc1e7a26527993a5fa2db87ca53a81fb15dca22a35d7f28c

Devnology is a non-profit organisation and thus depends on sponsors. Thanks to our wonderful sponsors all Devnology events are free!