Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is `unit` treated differently by the F# type system when used as a generic interface argument?

Tags:

f#

unit-type

Consider this interface:

type A<'a> =
    abstract X : 'a

Let's try to implement it with int as a generic argument:

{ new A<int> with member this.X = 5 } // all is well

Now, let's try unit for an argument:

// Compiler error: The member 'get_X : unit -> unit' does not have the correct type to override the corresponding abstract method.
{ new A<unit> with member this.X = () }

Now, if we define a non-generic interface, everything also works well:

type A_int =
    abstract X : int

{ new A_int with member this.X = 5 } // works

type A_unit =
    abstract X : unit

{ new A_unit with member this.X = () } // works as well!

Is there anything I can do to fix this problem?

like image 597
MisterMetaphor Avatar asked Oct 10 '14 09:10

MisterMetaphor


1 Answers

In F#, an abstract slot with declared return type of unit gets compiled in .NET IL as a return type of void. In contrast, an abstract slot with declared return type of "T" gets compiled in .NET IL as a generic return type of "T", which when T is instantiated by unit becomes unit'.

See : F# interface inheritance failure due to unit

like image 156
ArthurCPPCLI Avatar answered Oct 20 '22 15:10

ArthurCPPCLI