代码之家  ›  专栏  ›  技术社区  ›  Jules

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

  •  1
  • Jules  · 技术社区  · 15 年前

    我正在尝试创建一个接口,它需要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 和; Set 方法。问题是f编译器希望我实现抽象成员:

    没有为“abstract member varying.addhandler:handler<'t>->Unit”提供任何实现

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

    1 回复  |  直到 15 年前
        1
  •  6
  •   ssp    15 年前

    您可以添加 [<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