…from Data.Maybe
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.
Contents
- Constructing
- Pattern matching
- Testing instances
- Maybe.ap
- Equality testing
- Maybe.get
- Maybe.fromEither
- Maybe.toJSON and Maybe.fromJSON
Constructing
Constructing Maybe
values remains the same, but the import changes.
Previously:
Now:
Pattern matching
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 Maybe
is .matchWith(patterns)
, which works similarly, but passes an object with the fields instead of each value as positional argument:
Testing instances
Previously, Maybe
had isJust
and isNothing
boolean properties, which could be accessed to test whether a particular value was a Just
or a Nothing
:
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 null
or undefined
:
You can also test if a value is of a particular type by using the type’s .hasInstance
function:
Maybe.ap
The new applicativeFn.apply(applicativeValue)
method is the recommended way of using applicative functors now, which is standardised across Folktale and independent of Fantasy-Land changes.
.apply
and .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.
Equality testing
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 .equals
method that does a similar test, but compares values structurally if they’re Fantasy-Land setoids, arrays, or plain JavaScript objects:
More details can be found on the Equality derivation documentation.
Maybe.get
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:
Maybe.fromEither
Either
was replaced by Result
in Folktale 2, and so the new method is Maybe.fromResult
.
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 .toJSON()
and .fromJSON()
:
More details can be found on the Serialization derivation documentation.