Git Product home page Git Product logo

unknown6656.discriminatedunions's Introduction

Discriminated Unions

This library aims to bring discriminated unions to C#.
Discriminated unions (aka. "tagged unions", "disjoint unions", or "coproducts") are data structures which can hold a set of several different types. However, unions are designed to only hold one of the specified data types at any one time. (Read more: https://en.wikipedia.org/wiki/Tagged_union)

F#'s discriminated union:

type IntOrBool = Case0 of int
               | Case1 of bool

Discriminated unions such as the one above do not (yet) exist in C#. This project therefore aims to fill this gap by providing a set of generic union data types:

Union<int, bool> union1 = 315;
Union<int, bool> union2 = false;

if (union1.Is(out int i))
    Console.WriteLine("the union stores the integer value " + i);
else if (union1.Is(out bool b))
    Console.WriteLine("the union stores the boolean value " + b);

Installation

Use one of the follwing methods to install and use this library:

  • Package Manager:

    PM> Install-Package Unknown6656.DiscriminatedUnions
  • .NET CLI:

    > dotnet add package Unknown6656.DiscriminatedUnions
  • Package reference (e.g. in a .csproj/.vbproj/.fsproj project file):

    <PackageReference Include="Unknown6656.DiscriminatedUnions" Version="*" />
  • Paket CLI:

    > paket add Unknown6656.DiscriminatedUnions
  • F# Interactive:

    #r "nuget: Unknown6656.DiscriminatedUnions, *"

Documentation and Usage

To use the discriminated unions, simply include the namespace Unknown6656:

using Unknown6656;

Each union type Union<...> is an abstract record with a private constructor. The union cases are implemented using the sealed records Union<...>.Case*, which inherit from the class in which they are contained. Each Union<...>-type defines the following public symbols:

Symbol name and signature Symbol description
UnsafeItem : object [ TODO : Add description for the symbol ]
IsCase* : bool [ TODO : Add description for the symbol ]
AsCase* : T* [ TODO : Add description for the symbol ]
Equals : Union<...> -> bool [ TODO : Add description for the symbol ]
As<T> : () -> T [ TODO : Add description for the symbol ]
AsOrDefault<T> : () -> T [ TODO : Add description for the symbol ]
AsValueType<T> : () -> T [ TODO : Add description for the symbol ]
AsReferenceType<T> : () -> T? [ TODO : Add description for the symbol ]
Is<T> : () -> bool [ TODO : Add description for the symbol ]
Is<T> : () -> [out] T * bool [ TODO : Add description for the symbol ]
Match : (T0 -> T) * (T1 -> T) * ... -> T [ TODO : Add description for the symbol ]
Match : (T0 -> ()) * (T1 -> ()) * ... -> () [ TODO : Add description for the symbol ]
ToString : () -> string [ TODO : Add description for the symbol ]
[static] FromObject : object -> Union<...> [ TODO : Add description for the symbol ]
[static] operator == : Union<...> * Union<...> -> bool [ TODO : Add description for the symbol ]
[static] operator != : Union<...> * Union<...> -> bool [ TODO : Add description for the symbol ]
[static, implicit] operator : T* -> Union<...> [ TODO : Add description for the symbol ]
[static, implicit] operator : Union<...> -> T* [ TODO : Add description for the symbol ]

Each union case overrides the majority of the mentioned symbols and [TODO]. Each case further defines the following symbols:

Symbol name and signature Symbol description
Item : T* [TODO : Add description for the symbol]

[TODO]

Code samples

unknown6656.discriminatedunions's People

Contributors

unknown6656 avatar

Stargazers

John Louderback avatar Daniel Blendea avatar dzmitry-lahoda avatar Grand Silence avatar Dmitry Ponomarev avatar  avatar  avatar Viktor Svub avatar  avatar Konrad Jamrozik avatar

Watchers

James Cloos avatar  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.