Using the device as a type parameter and overriding

I basically ask why the following lines of code do not compile:

type IGenericType<'a> = 
    abstract member MyFunc : 'a -> 'a -> 'a

type Implementer() = 
    member x.Test () () = () // unit->unit->unit
    interface IGenericType<unit> with 
        member x.MyFunc a b = b // FS0017
        // member x.MyFunc () () = () // FS0017

Just wondering if there is a way to do this work as intended. I assume this is a limitation associated with the implementation of units and generics.

Im using the following workaround at the moment:

type Nothing = 
    | Nothing
type Implementer() = 
    interface IGenericType<Nothing> with 
        member x.MyFunc a b = b

Hope someone can highlight this behavior a bit.

+2
source share
1 answer

You guessed it right. For interoperability within the .NET framework, the F # compiler does not emit IL for unit, but instead replaces it with void.

, unit. ; .

F # - .

+2

Source: https://habr.com/ru/post/1659056/


All Articles