Вопрос

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)
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top