Skip to content

binarycow/Union

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

9 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

C# library for unions. Similar to OneOf, with a few conceptual changes.

  • Better handling of nullable reference types
  • Change methods to match C# idioms, rather than adopting F# idioms.

Examples

private static void Main()
{
    ReadFileText("C:\\Path\\to\\file.txt").Switch(
        _ => Console.WriteLine($"File doesn't exist."),
        error => Console.WriteLine($"Error: {error.Value}"),
        contents => Console.WriteLine($"File contents: {contents}")
    );
}

private static Union<None, Error<string>, string> ReadFileText(string path)
{
    try
    {
        if (File.Exists(path) == false)
            return new None();
        return File.ReadAllText(path);
    }
    catch (Exception e)
    {
        return new Error<string>(e.Message);
    }
}

Nulls

Null values are not allowed.

If you have the need to return a null value, I suggest a different pattern - using the None type instead of returning null. See the above example - instead of returning null, Union.None is returned. Thus, all other options are guarenteed to not be null.

About

No description, website, or topics provided.

Resources

License

Stars

11 stars

Watchers

1 watching

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages