as another option
type Suit = |Clubs = 'C' |Spades = 'S' |Hearts = 'H' |Diamonds = 'D' let c = Suit.Clubs let v : char = LanguagePrimitives.EnumToValue c
Editorial: Comparison of different approaches:
type Suit = |Clubs = 'C' |Spades = 'S' |Hearts = 'H' |Diamonds = 'D' let valueOf1 (e : Suit) = LanguagePrimitives.EnumToValue e let valueOf2 (e : Suit) = unbox<char> e let valueOf3 (e : Suit) = (box e) :?> char
And under the hood:
.method public static char valueOf1 ( valuetype Program/Suit e ) cil managed { // Method begins at RVA 0x2050 // Code size 3 (0x3) .maxstack 8 IL_0000: nop IL_0001: ldarg.0 IL_0002: ret } // end of method Program::valueOf1 .method public static char valueOf2 ( valuetype Program/Suit e ) cil managed { // Method begins at RVA 0x2054 // Code size 13 (0xd) .maxstack 8 IL_0000: nop IL_0001: ldarg.0 IL_0002: box Program/Suit IL_0007: unbox.any [mscorlib]System.Char IL_000c: ret } // end of method Program::valueOf2 .method public static char valueOf3 ( valuetype Program/Suit e ) cil managed { // Method begins at RVA 0x2064 // Code size 13 (0xd) .maxstack 8 IL_0000: nop IL_0001: ldarg.0 IL_0002: box Program/Suit IL_0007: unbox.any [mscorlib]System.Char IL_000c: ret } // end of method Program::valueOf3
source share