Data.Maybe provided a way of modelling computations that may provide a value or not. Folktale 2 keeps the same structure, but there’re some changes in how you extract values out of Maybes and test them. You can look at the full documentation for Maybe for more detailed information.
- Pattern matching
- Testing instances
- Equality testing
- Maybe.toJSON and Maybe.fromJSON
Maybe values remains the same, but the import changes.
Previously it was possible to pattern match on a
Maybe value by using the
.cata(patterns) method. This method would take the value inside of the data structure and pass it on as positional arguments to the proper function:
Now, the preferred method in
.matchWith(patterns), which works similarly, but passes an object with the fields instead of each value as positional argument:
isNothing boolean properties, which could be accessed to test whether a particular value was a
Just or a
Folktale 2 replaces these with a
.hasInstance(value) function on the variant constructors and on the type. Variant testing can now be safely done with this function, including on values that may be
You can also test if a value is of a particular type by using the type’s
applicativeFn.apply(applicativeValue) method is the recommended way of using applicative functors now, which is standardised across Folktale and independent of Fantasy-Land changes.
.ap still have the same semantics, but those semantics are different from the new
fantasy-land/ap function! In order to write functions that are generic over different Fantasy-Land implementations and versions, the new fantasy-land module should be used instead.
Previously Maybe had a
.isEqual method, which checked if two maybe values had the same tag and the same value (compared by reference):
Now, Maybe and other Folktale structures have a
More details can be found on the Equality derivation documentation.
Previously, Maybe had a
.get() method that would extract the value out of a Just structure, but throw an error if you had a Nothing structure:
This was unsafe, so in order to clearly signal that Folktale 2 has deprecated all
.get() methods, and introduced a new
.unsafeGet() one. You may use the new
.unsafeGet() one if you really know what you’re doing, but the method name now signals that you should be careful with it:
You’re strongly encouraged to use the
.getOrElse(default) method instead, which does not suffer from the same partiality problem:
Either was replaced by
Result in Folktale 2, and so the new method is
Please see the Migrating from Data.Either documentation if you have Either values in your code base.
Maybe.toJSON and Maybe.fromJSON
There was some experimental support in the old
Maybe structure for serialisation to and parsing from JSON structures:
The new Folktale has a more standardised (and general) way of serialising and parsing its algebraic data structures, so each structure provides its own
More details can be found on the Serialization derivation documentation.