Is there an equivalent to creating a C# implicit operator in F#?

As others have pointed out, there is no way to do implicit conversion in F#. However, you could always create your own operator to make it a bit easier to explicitly convert things (and to reuse any op_Implicit definitions that existing classes have defined):

let inline (!>) (x:^a) : ^b = ((^a or ^b) : (static member op_Implicit : ^a -> ^b) x)

Then you can use it like this:

type A() = class end
type B() = static member op_Implicit(a:A) = B()

let myfn (b : B) = "result"

(* apply the implicit conversion to an A using our operator, then call the function *)
myfn (!> A())

Implicit conversion is rather problematic with respect to type safety and type inference, so the answer is: No, it actually would be a problematic feature.


No, there is not.

Tags:

C#

F#