【发布时间】:2016-11-16 09:34:27
【问题描述】:
我有一个愚蠢的问题,我需要在 F# 类中实现具有以下方法的接口:
public interface IMyInterface
{
T MyMethod<T>() where T : class;
}
我在 F# 中苦苦挣扎。我尝试了不同的方法。但问题是必须返回 T 类型的对象。不接受空值:
type public Implementation() =
interface IMyInterface with
member this.MyMethod<'T when 'T : not struct>() = null
错误:成员 'MyMethod : unit -> a' when a': not struct 和 'a: null 没有正确的类型来覆盖相应的抽象方法。所需的签名是 'MyMethod : unit -> 'T when 'T: not struct'
所以我尝试将 T 作为类的参数,但仍然没有我完成错误:
type public Implementation(data : 'T when 'T : not struct) =
interface IMyInterface with
member this.MyMethod<'T when 'T : not struct>() = data
错误:成员 'MyMethod : unit -> 'T when 'T : not struct' 没有正确的类型来覆盖相应的抽象方法。 em>
感谢您的帮助。
【问题讨论】:
标签: interface f# type-constraints