Code for a lightning talk on making illegal states unrepresentable in C# and F#.
The code follows Scott Wlashin's excellent Designing with types blog series.
There are tests for each language, but these don't assert that the code is correct, they just show the shape of the data in the console:
Start at the main branch, then follow the branches by number.
This is the starting point. It shows a naive implementation of a contact type and how that's not safe.
public record Contact
{
public string FirstName { get; init; } = "";
public string MiddleInitial { get; init; } = "";
public string LastName { get; init; } = "";
public string EmailAddress { get; init; } = "";
public bool IsEmailVerified { get; init; }
public string Address1 { get; init; } = "";
public string Address2 { get; init; } = "";
public string City { get; init; } = "";
public string State { get; init; } = "";
public string Zip { get; init; } = "";
public bool IsAddressValid { get; init; }
}- Designing with types
- Yaron Minsky from Jane Street (who coined the phrase 'making illegal states unrepresentable')
- Sudhir Mangla's article on Developers Voice which take it further using C#
- Railway oriented programming in F# (follows up from the last section of Sudhir's blog above)
- Vogen
- Kahlid Amuhakmeh's blog about Vogen
- Briain Chavez's wonderful Bogus library which generates the data for us
Scott Wlashin's series is really about domain modelling. The last section of the series presents a more meaningful challenge:
“A contact must have at least one of the following: an email, a postal address, a home phone, or a work phone”_
https://fsharpforfunandprofit.com/posts/designing-with-types-discovering-the-domain/
His book is excellent too!


