Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
.net 使用unit作为类型参数和重写方法_.net_F# - Fatal编程技术网

.net 使用unit作为类型参数和重写方法

.net 使用unit作为类型参数和重写方法,.net,f#,.net,F#,我基本上是在问为什么以下代码行不能编译: 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

我基本上是在问为什么以下代码行不能编译:

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
类型IGenericType'a->'a
类型实现器()=
成员x.Test()=()//单元->单元->单元
IGenericType与的接口
成员x.MyFunc a b=b//FS0017
//成员x.MyFunc()=()//FS0017
只是想知道是否有一种方法可以让这项工作如期进行。我假设这是一个限制,必须与单元和泛型的实现相关

目前,我正在使用以下解决方法:

type Nothing = 
    | Nothing
type Implementer() = 
    interface IGenericType<Nothing> with 
        member x.MyFunc a b = b
type Nothing=
|没什么
类型实现器()=
IGenericType与的接口
成员x.MyFunc a b=b

希望有人能对这种行为有所了解。

你猜对了。对于.NET framework内部的互操作性,F#编译器不会为
unit
发出IL,而是将其替换为
void

因此,您的通用接口适用于除
单元
以外的任何类型。这似乎不是什么大问题;对于这个问题,您的变通方法是一个很好的解决方案

有关更详细的讨论,请参阅