Skip to content
/ Option Public

Option to replace Nullable as in functional programming for Unity.

License

MIT, Unknown licenses found

Licenses found

MIT
LICENSE
Unknown
LICENSE.meta
Notifications You must be signed in to change notification settings

CorbyO/Option

Repository files navigation

Option

Option to replace Nullable as in functional programming for Unity.

Table of Contens

Install

Via Git

add "com.corby.option": "https://github.com/CorbyO/Option.git" to Packages/manifest.json.

Via OpenUPM

openupm add com.corby.option

Getting started

Initialize

Option<int> num = new(); // None<int>
Option<int> num = 3.ToSome(); // Some<>(3)
Option<Transform> parent = transform.parent.ToOption(); // Some<> or None 

Return without null

public static Option<Transform> GetParent<T>(this Transform transform)
{
    return transform.parent.ToOption();
}

Unwarpping

var parentName = transform.GetParent().Unwrap() switch
{
    Some<Transform> parent => parent.name,
    None<Transform> => "None"
};

Serialization

image

This is a simple example of how to serialize a class or struct that contains Option.

[Serializable]
public struct TestStruct
{
    public Option<int> Number;
    public Option<string> Name;
    public Option<Vector3> Position;
}

[Serializable]
public class TestClass
{
    public Option<Sprite> Sprite;
    public Option<List<int>> Numbers;
}


public class TestComponent : MonoBehaviour
{
    public Option<TestStruct> Struct;
    public Option<TestClass> Class;
}

About

Option to replace Nullable as in functional programming for Unity.

Topics

Resources

License

MIT, Unknown licenses found

Licenses found

MIT
LICENSE
Unknown
LICENSE.meta

Stars

Watchers

Forks

Packages

No packages published

Languages