hubFS: THE place for F#

. . . are you on The Hub?
Welcome to hubFS: THE place for F# Sign in | Join | Help
in Search

interface methods

Last post 08-10-2006, 5:11 by Tejus. 2 replies.
Sort Posts: Previous Next
  •  08-10-2006, 4:33 470

    interface methods

    Hi,

    I'm working with interfaces, but am unsure on how to go about calling the methods defined in the interface - take the example of the widget/wodget class that is given as an example when an fs file is added:

    type IPeekPoke = interface
      abstract member Peek: unit -> int
      abstract member Poke: int -> unit
    end

    type wodget = class
      val mutable state: int
      interface IPeekPoke with
        member x.Poke(n) = x.state <- x.state + n
        member x.Peek() = x.state
      end
      member x.HasBeenPoked = (x.state <> 0)
      new() = { state = 0 }
    end

    let a = new wodget();;

    Now, if i say a.Peek(), i get an error stating that Peek is not defined. I'm assuming it's just a simple case of getting the syntax right?

    Thanks,
    Tejus

  •  08-10-2006, 4:45 471 in reply to 470

    Re: interface methods

    Use an upcast:

    (a :> IPeekPoke).Peek()

    Cheers,

    Don

     

  •  08-10-2006, 5:11 472 in reply to 471

    Re: interface methods

    That works - thanks!
View as RSS news feed in XML
Powered by Community Server, by Telligent Systems