views:

146

answers:

1

I'm trying to create an interface that requires additional methods on top of IEvent, like this:

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)

I added the Get & Set methods. The problem is that the F# compiler wants me to implement the abstract members:

No implementation was given for 'abstract member Varying.AddHandler : Handler<'t> -> unit'

But the point of this type is that the member is abstract. What am I doing wrong?

+4  A: 

You can add [<AbstractClass>] label to your definition:

> [<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
ssp