Skip to content

Sammmte/Paps-Maybe

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

9 Commits
 
 
 
 
 
 

Repository files navigation

Paps Maybe

What is it?

Paps Maybe is just another implementation of Maybe Monads in c#.

For what purpose?

Prevent null on reference types and default() on value types. Maybes helps handle the lack of value in a expressive way.

How to install it

  1. The package is available on the openupm registry.
openupm add paps.maybe

How to use it

Anything can be a Maybe

void SomeMethod(SomeClass probablyNullObject)
{
    var maybe = probablyNullObject.ToMaybe();

    maybe.Do(value => Debug.Log("If you see this, the value was not null!"));
}

Use its operators

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
}

License

MIT License

About

Maybe object and related utilities in c#

Resources

Stars

Watchers

Forks

Packages

 
 
 

Contributors

Languages