Enumeration type extension in F #

Another issue related to the F # function called "Type Extensions" .

It seems impossible to expand the enumerations in F #. I often use C # Extensions Methods to expand enumerations: adds range checking logic, a method that returns a string representation, etc.

Unfortunately, it seems possible to expand only the discriminated union, but it is impossible to expand simple enumerations :

1. Internal expansion

// CustomEnum.fs
module CustomEnumModule

type CustomEnum = 
    | Value1 = 1
    | Value2 = 2

// Trying to split definition of the enum
type CustomEnum with 
    | Value3 = 3

Error: 'error FS0010: Unexpected character' | ' in the definition of a member

2. Additional extension

// CustomEnumEx.fs
open CustomEnumModule

type CustomEnum with
    member public x.PrintValue() =
        printfn "%A" x

Error: "error FS0896: enumerations cannot have members"

, (1) , (2) .NET enums - ( FP-) .

, ?

P.S. F # Spec , , , .

+5
1

, , :

type CustomEnum = Value1 = 1 | Value2 = 2

[<CompilationRepresentation(CompilationRepresentationFlags.ModuleSuffix)>]
module CustomEnum =
    let Print = function
    | CustomEnum.Value1 -> "One"
    | CustomEnum.Value2 -> "Two"
    | _ -> invalidArg "" ""

let value = CustomEnum.Value1

let s = CustomEnum.Print value
+7

All Articles