How can I duplicate the F# discriminated union type in C#?

后端 未结 6 955
抹茶落季
抹茶落季 2020-12-09 18:10

I\'ve created a new class called Actor which processes messages passed to it. The problem I am running into is figuring out what is the most elegant way to pass related but

6条回答
  •  孤城傲影
    2020-12-09 18:59

    If you have this

    type internal Either<'a, 'b> =
      | Left of 'a
      | Right of 'b
    

    in F#, then the C# equivalent of the CLR generated for class Either<'a, 'b> has inner types like

    internal  class _Left : Either
    {
         internal readonly a left1;
         internal _Left(a left1);
    }
    

    each with a tag, a getter and a factory method

    internal const  int tag_Left = 0;
    internal static  Either Left(a Left1);
    internal a Left1 {  get; }
    

    plus a discriminator

    internal int  Tag { get; }
    

    and a raft of methods to implement interfaces IStructuralEquatable, IComparable, IStructuralComparable

提交回复
热议问题