Working with Maybe
"The Maybe type encapsulates an optional value. A value of type Maybe 'T either contains a value of type 'T, or it is empty." - Haskell documentation
The Maybe type in Wooga.Lambda borrows its name from Haskell: The empty Maybe case is called Nothing and the Maybe holding a value is called Just.
Maybe instance
Create a Maybe by injecting a value with Maybe.Just('T) or create the empty one with Maybe.Nothing<'T>().
1: 2: |
|
Use the FromMaybe('T) or FromJustOrDefault('T2,'T->'T2) functions to extract the Just value; both will fallback to the given default value in case of Nothing.
1: 2: 3: 4: 5: |
|
Check out the API Reference for more information.
Avoiding null and NullReferenceExceptions
Avoid using null to represent empty values. The following snippet is valid code and will raise a NullReferenceException.
1: 2: |
|
By using Maybe you make sure that the potential empty case will be handled at runtime.
1: 2: |
|
Composing Maybe
Because Maybe is a monad, composing multiple of them is very straightforward. The monadic operators >>= & >> are implemented as Maybe.Bind and Maybe.Then.
1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: |
|