Giter VIP home page Giter VIP logo

option's Introduction

Option

What is it?

An Option type for C#. Options are a well studied Functional paradigm that allows for the representation of not having a value (None) as well as having a value (Some). This allows for a clear differentiation without the need for null values.

How can I get it?

Option is available as a NuGet package: https://www.nuget.org/packages/Option

PM> Install-Package Option

Why was it made?

Option types are missing as a built in for C#, and while present and excellent in F# using F# options in C# is cumbersome. There are decent solutions which use extension methods or wrapper classes, but these solutions are not ideal because:

  1. You need to include the FSharp.Core runtime dll in your project
  2. The representation of FSharpOption.None is actually null. The F# language has built in compiler constructs to handle this, but in C# whatever interop library you're using must take care of it.

Decompiled Source for FSharpOption.None

/// <summary>
/// Create an option value that is a 'None' value.
/// </summary>
[CompilerGenerated]
[DebuggerNonUserCode]
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
public static FSharpOption<T> None
{
    [CompilationMapping(SourceConstructFlags.UnionCase, 0)] get
    {
        return (FSharpOption<T>) null;
    }
}

Example Usage

Using the value

public void Example1()
{
    Option<int> option = 1;
    
    // Most Performant
    int value;
    if (option.TryGetValue(out value))
    {
        // Do something with the value
        Console.WriteLine(value);
    }
    else
    {
        // Do something else
    }
}
public void Example2()
{
    Option<int> option = 1;
    
    // Second Most performant
    if (option.HasValue)
    {
        // Do something with the value
        Console.WriteLine(option.Value);
    }
    else
    {
        // Do something else
    }
}
public void FunctionalExample1()
{
    Option<int> option = 1;
    
    // Third most performant
    option.Match(
        None: () => { /* Do nothing */ }
        Some: value =>
        {
            //Do something with the value
            Console.WriteLine(value);
        });
}
public void FunctionalExample2()
{
    Option<int> option = 1;
    
    // Least performant but can match on values
    option.Match()
        .None(() => Console.WriteLine("No value"))
        .Some(1, () => Console.WriteLine("One"))
        .Some((value) => Console.WriteLine(value))
        .Result();
}

Getting the value

public void GetExample()
{
    Option<int> option = 1;
    
    // Getting the value out
    var v = option.ValueOrDefault;
    var v2 = option.ValueOr(0);
    var v3 = option.ValueOr(() => 0);
}
public void FunctionalGetExample1()
{
    Option<int> option = 1;
    
    // More performant, but can't match on exact values
    var match1 = option.Match(
        None: () => "No value"
        Some: (value) => value.ToString());
}
public void FunctionalGetExample2()
{
    // Less performant but can match on values
    var match2 = option.Match<string>()
        .None(() => "No value")
        .Some(1, () => "One")
        .Some((value) => value.ToString())
        .Result();
}

Road Map

  • Interop support with FSharpOption

option's People

Contributors

tejacques avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.