The History of fpdart | Functional programming in Dart

Sandro Maglione

Sandro Maglione

Mobile development

The interest in functional programming has been growing rapidly over the last few years.

As the dart language itself evolves, more and more people are becoming aware of the advantages of adopting a functional approach: making the code easier to test and maintain.

In this post I will walk you through how fpdart came to be. We are going to explore the history of functional programming in dart, the origin of the dartz package, and how fpdart was born 👇


Before fpdart: the dartz package

Back in 2020 the only solution for functional programming in dart was dartz.

The work on dartz started on 11 February 2016 (first commit). At the time the latest version of dart was v1.14.2, released the day before, on 10 February 2016.

It took more than 2 years for the release of dart v2.0.0. Dart 2 was a major update that introduced a sound static type system.

dartz was initially implemented for dart v1. The migration to dart 2 started soon after the new release (reference commit).

The problem with dartz: Documentation

One of the major pain points of dartz has always been documentation. As functional programming started to become more popular, more people were interested in using the library.

Nonetheless, dartz was lacking documentation, with no way for a beginner to know how to get started and how to use the API.

On February 2020 an issue on the dartz repository started a discussion to expand the documentation of the library for all types.

Many people were willing to contribute, but after more than a year no real progress was made.

How fpdart came to be

In the same period I was starting to use dartz myself in all of my projects. My approach was looking directly at the source code to understand how to use the API.

Nonetheless, I knew that not everyone could do the same.

I was proficient enough in both functional programming and dart to be able to understand the source code, but a beginner in either one of the two would have had an hard time doing the same.

Furthermore, by looking daily at the source code I started noticing some improvements that could be made to the library. I then decided to open an issue on the dartz repository to discuss the future of the library and how to contribute (5 March 2021).

The author of dartz (spebbe) responded some months later (when fpdart was already released) highlighting the state of the library:

dartz-issue-repository-fpdartdartz-issue-repository-fpdart

"On the other hand, maybe that time would be better spent contributing to fpdart instead?"

There is more.

Every week I build a new open source project, with a new language or library, and teach you how I did it, what I learned, and how you can do the same. Join me and other 600+ readers.

Functional programming in dart: how to?

The first commit of fpdart dates 28 May 2021, nearly 1 month after I opened the issue on the dartz repository.

This initial work aimed to investigate an alternative approach for functional programming in dart. Most of the first typeclasses were heavily inspired by cats, a functional programming library in scala.

The major roadblock was the Option type. The reason is that dart does not support higher kinded types. That's why on 30 May 2021 I opened an issue on the dart repository to discuss adding higher kinded types to the language:

dart-higher-kinded-types-issuedart-higher-kinded-types-issue

Higher kinded types in dart

I started researching how to solve this issue. I knew that other libraries (fp-ts) where implementing higher kinded types even without native support in the language.

After a week of studies, I finally found the solution. Specifically, I used a technique know as Defunctionalization (from the paper "Lightweight higher-kinded polymorphism").

I also found 2 articles that are still referenced in fpdart today (hkt.dart):

On June 5 2021 higher kinded types landed on fpdart:

/// - [A]: Type parameter
/// - [G]: Type constructor
///
/// Instead of writing `G<A>`, we will write `HKT<G, A>`.
/// This is useful because in the expression `HKT<G, A>`, both `G` and `A` have kind `*`,
/// so we can deal with them with the type system we currently have at our disposal.
abstract class HKT<G, A> {}

HKT became the core of all types in fpdart. I then implemented the Option type as follows:

/// Tag the `HKT` interface for the actual `Maybe`
abstract class MaybeHKT {}
 
abstract class Maybe<A> extends HKT<MaybeHKT, A>
    with Applicative<MaybeHKT, A>, Foldable<MaybeHKT, A>

Fun fact: In fpdart the Option type was initially called Maybe (coming from Haskell). It was then renamed to Option after receiving a feedback on Twitter by Gaëtan Schwartz (commit) 👇

Sharing fpdart

Around the same time I wrote a thread on Twitter to introduce the library and functional programming in dart:

This tweet became popular in the dart community. It is one of the main starting points that made people aware and excited about fpdart and functional programming in dart.

Not long afterward people started contributing to the library. My goal was to extend and expand the API to include all the types used in functional programming.

Hint 💡: You can take a look at the CHANGELOG, which includes all the releases and their dates

Documentation in fpdart

With fpdart my goal from the beginning was to make a functional programming library that was accessible to everyone.

In order to achieve this goal, documentation is key.

Therefore, since day 1, no new class or type was added without documentation. All the API (classes, methods, extensions) required a documentation comment before being accepted as completed inside fpdart.

The documentation of fpdart is one of the main factor that allowed the library to gain traction in the community.

Part of my time was also spent writing blog posts explaining the main features of fpdart and functional programming in general (take a look at the first post introducing fpdart).

What fpdart is today

Today, more than 2 years afterward, fpdart is now the standard solution for functional programming in dart.

On 26 July 2023, fpdart v1 is out, bringing support for all Dart 3 new features, new types (ReaderTaskEither), and a complete refactoring of many parts of the codebase.

There is still a lot of room for improvement, especially when it comes to teaching people how to approach and make the best use of the library and functional programming in general.

I will continue to make fpdart better over time with the help of the community. You can expect much more to come in the future.

In the meantime, you can start using fpdart today.

Happy coding 👋


By the way, if you are interested in the receiving the latest updates on fpdart, as well as tips and code snippets on functional programming and much more, you can subscribe to my newsletter here below 👇

👋・Interested in learning more, every week?

Every week I build a new open source project, with a new language or library, and teach you how I did it, what I learned, and how you can do the same. Join me and other 600+ readers.