我基本上问为什么下面的代码行不能编译:
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
只是好奇是否有方法使这项工作按预期进行。我认为这是一个限制,必须与单位和泛型的实施。
我现在使用以下解决方法:
type Nothing =
| Nothing
type Implementer() =
interface IGenericType<Nothing> with
member x.MyFunc a b = b
希望有人能为这种行为带来一些启示。
答案 0 :(得分:2)
你猜对了。对于.NET框架内的互操作性,F#编译器不会为unit
发出IL,而是用void
替换它。
因此,您的通用界面适用于unit
以外的任何类型。这似乎不是一个大问题;你的解决方法是解决这个问题的一个很好的解决方案。
可以在F# interface inheritance failure due to unit中找到更详细的讨论。