Paps Maybe is just another implementation of Maybe Monads in c#.
Prevent null on reference types and default() on value types. Maybes helps handle the lack of value in a expressive way.
- The package is available on the openupm registry.
openupm add paps.maybe
void SomeMethod(SomeClass probablyNullObject)
{
var maybe = probablyNullObject.ToMaybe();
maybe.Do(value => Debug.Log("If you see this, the value was not null!"));
}
Maybe<int> SomeMethod(int myValueIKnowIsInvalid)
{
return myValueIKnowIsInvalid.AsNothing();
}
Maybe<int> SomeMethod(int myValueImNotSureIsInvalid)
{
return myValueImNotSureIsInvalid.AsNothingWhen(() => myValueImNotSureIsInvalid < 0);
}
int SomeMethod(Maybe<int> myMaybeValueWhichICanDefault)
{
return myMaybeValueWhichICanDefault.GetOrDefault(someOtherDefaultValue);
}
void DoSomethingIfHasValue(Maybe<int> maybe)
{
//DoSomething will not execute if it does not has value
maybe.Do(() => DoSomething())
.OrElse(() => DoSomethingElse()); //DoSomethingElse will not execute if it has value
}