How to define explicit operator in F#?
Вопрос
How do you implement the equivalent of C#'s explicit
operator in F#? Is it supported?
Решение
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)
Не связан с StackOverflow