In F#, all interface implementations are explicit, which means that the C# analog of your code would be

1
2
3
4
5
6
public class Base2 : IFace2 {
    void IFace2.DoIt() { 
        Console.WriteLine("In Base2.DoIt");
    }
    ...
}

So you'll have to add an additional public virtual method to your class so that subclasses can override it - this can use the same name as the interface's method.  Declaring a virtual method in F# is slightly tricky: you declare an abstract method slot and then define the initial implementation:

1
2
3
4
5
6
7
8
type Base2 () =
    interface IFace2 with
        member x.DoIt () = x.DoIt()
    abstract DoIt : unit -> unit
    default x.DoIt() = printfn "In Base2.DoIt"
    member x.Print a = 
        (x :> IFace2).DoIt ()
        printfn "In Base2.Print"

Here I've made the interface call into the virtual method so that overriding it in a derived class will also change the behavior when an instance of the derived class is upcast to the interface.

Hope that helps.

-Keith

By on 10/5/2011 1:29 PM ()
IntelliFactory Offices Copyright (c) 2011-2012 IntelliFactory. All rights reserved.
Home | Products | Consulting | Trainings | Blogs | Jobs | Contact Us
Built with WebSharper