Question

How do you implement the equivalent of C#'s explicit operator in F#? Is it supported?

Était-ce utile?

La solution

Just implement an op_Explicit static member like

type SomeType() =
    static member op_Explicit(source: SomeType) : int =
        1

and then you can use a corresponding F# explicit conversion operator like

SomeType() |> int

you can see a bit into how this works by noting the static member constraint on the type signature of int

^a -> int when  ^a : (static member op_Explicit :  ^a -> int)
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top