如何在F#中使用其他方法创建接口

时间:2009-07-21 18:52:23

标签: .net f# interface typing

我正在尝试创建一个在IEvent之上需要其他方法的接口,如下所示:

type Varying<'t> = 
    abstract member Get : unit -> 't
    abstract member Set : 't -> unit
    abstract member AddHandler : Handler<'t> -> unit
    abstract member RemoveHandler : Handler<'t> -> unit
    member v.Add(f) = v.AddHandler(new Handler<_>(fun _ x -> f x))
    interface IEvent<'t> with
        member c.AddHandler(h) = c.AddHandler(h)
        member c.RemoveHandler(h) = c.RemoveHandler(h)
        member c.Add(f) = c.Add(f)

我添加了Get&amp; Set方法。问题是F#编译器要我实现抽象成员:

  

没有给出'抽象成员Varying.AddHandler:Handler&lt;'t&gt;的实现。 - &GT;单元“

但这种类型的观点是该成员是抽象的。我做错了什么?

1 个答案:

答案 0 :(得分:6)

您可以在定义中添加[<AbstractClass>]标签:

> [<AbstractClass>]
- type Varying<'t> =
-     abstract member Get : unit -> 't
-     abstract member Set : 't -> unit
-     abstract member AddHandler : Handler<'t> -> unit
-     abstract member RemoveHandler : Handler<'t> -> unit
-     member v.Add(f) = v.AddHandler(new Handler<_>(fun _ x -> f x))
-     interface IEvent<'t> with
-         member c.AddHandler(h) = c.AddHandler(h)
-         member c.RemoveHandler(h) = c.RemoveHandler(h)
-         member c.Add(f) = c.Add(f)
- ;;

type Varying<'t> =
  class
    abstract member AddHandler : Handler<'t> -> unit
    abstract member Get : unit -> 't
    abstract member RemoveHandler : Handler<'t> -> unit
    abstract member Set : 't -> unit
    interface IEvent<'t>
    member Add : f:('t -> unit) -> unit
  end