Also note that the object is automatically upcast when you use it as a parameter:

let doSomethingWithInterface (i:IIncrement) = i.M(10)

let c =C()

printfn "%d" (doSomethingWithInterface c)

So in real code, you do not need upcasts as often as it might seem at first.

By on 9/22/2010 6:29 PM ()

My understanding is that C# has some syntactic sugar for interfaces: when you implement an interface in C# it automatically adds a method on the class which calls the interface method for you (unless you tell it not to by using C#'s explicit interface syntax). The CLR, and F#, don't provide this syntactic sugar so you have to do it manually.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
type IIncrement = 


    abstract M : int -> int 

type C() = 


    interface IIncrement with 


        member x.M(n) = n + 1 


    member this.M(n) = (this :> IIncrement).M n

let c = C() 


c.M(10) // works

Typically, though, if you're not interoperating with C# you wouldn't bother to do this, you'd just up-cast at the point of use.

-Max

By on 9/22/2010 5:12 PM ()

thanks!

By on 10/13/2010 8:36 AM ()

Interface implementation in F# is explicit : you have to upcast with :> to access the members.

1
2
3
4
let ci = C() :> IIncrement

ci.M(10)
By on 9/9/2010 6:27 AM ()

(>..<)

<) thank you~

By on 9/9/2010 8:41 AM ()
IntelliFactory Offices Copyright (c) 2011-2012 IntelliFactory. All rights reserved.
Home | Products | Consulting | Trainings | Blogs | Jobs | Contact Us | Terms of Use | Privacy Policy | Cookie Policy
Built with WebSharper