The MSIL that is generated for TypeA and TypeB are slightly different. TypeA only explicitly implements IB while TypeB explicitly implements IA and IB. Functionally, however, they work the same:

1
2
3
4
5
6
7
8
9
10
11
12
13
let a1 = TypeA() :> IA
let b1 = TypeB() :> IA
printfn "%d" (a1.A())
// 1
printfn "%d" (b1.A())
// 1

let a2 = TypeA() :> IB
let b2 = TypeB() :> IB
printfn "%d" (a2.A())
// 1
printfn "%d" (b2.A())
// 1

Here is the same kind of example using object expressions (I think you had a typo in your definition of IB corrected below):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
type IA =
    abstract A : unit -> int

type IB =
    inherit IA
    abstract B : unit -> int
    
let a =
    { new IA with
        member this.A() = 1 }
        

let b =
    { new IB with
        member this.A() = 1
        member this.B() = 2 }

printfn "%d" (a.A())
// 1
printfn "%d" (b.B())
// 2

I hope that answers your questions!

By on 9/5/2008 7:59 AM ()

Thanks [b]RayV[/b] for your answers.

How can I implement the second group of interfaces using object expressions, where one interface inheriting from another defines a method with the same name/signature (it wasn't a typo)?

1
2
3
4
5
6
7
8
9
10
type IA =

    abstract DoWork : unit -> int


type IB =

    inherit IA

    abstract DoWork : unit -> int

Thanks again!

By on 9/5/2008 8:35 AM ()

Glad to help!

I think I understand now. Is this the example you're looking for?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
type IA =
    abstract DoWork : unit -> int


type IB =
    inherit IA
    abstract DoWork : unit -> int


let x =
    { new IB with
            member this.DoWork() = 2
        interface IA with
            member this.DoWork() = 1 }


let a = x :> IA
printfn "%d" (a.DoWork())
// 1


let b = x
printfn "%d" (b.DoWork())
// 2

Use the derived type as the type of the object expression, then explicitly implement the inherited type below that.

By on 9/5/2008 10:17 AM ()

That was what I was looking for. Thanks a lot!

By on 9/5/2008 10:49 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