Hi there,

F# 1.9.4 only supports "explicit" interface implementations. Implicit interface implementations have been requested and it is likely we'll support them in a future version of F#.

However, it's also worth learning other techniques for partial implementations of objects. For example, you can simulate many partial implementation techniques through function parameters, object expressions and/or optional arguments. For example, consider the following

1
2
3
4
5
6
7
8
9
10
11
12
 

type IProduct =
    abstract Name: string
    abstract Work: unit -> unit

let BuildProduct(name) =
    { new IProduct with 
        member x.Name = name 
        member x.Work() = printfn "work" }

Here an object expression has been used for your original request (Name variable, Work fixed). More generally, a object builder parameterized by optional arguments with sensible defaults can be quite flexible :

1
2
3
4
5
6
7
8
9
10
type ProductBuilders = 
    static member BuildProduct(?name,?work) =
        { new IProduct with 
            member x.Name = match name with None -> "" | Some n -> n
            member x.Work() = match work with None -> () | Some f -> f() }


ProductBuilders.BuildProduct(name="1")
ProductBuilders.BuildProduct(work=(fun () -> printfn "work"))
ProductBuilders.BuildProduct(name="3",work=(fun () -> printfn "work"))

You may also define types that implement an interface in a similar way:

1
2
3
4
5
6
7
8
9
10
11
 

type Product(?name,?work) = 
    interface IProduct with 
            member x.Name = match name with None -> "" | Some n -> n
            member x.Work() = match work with None -> () | Some f -> f()  


let p = (Product(name="1") :> IProduct)

Kind regards

don

By on 6/27/2008 4:29 PM ()

Thanks don! I have been reading your book, <i>Expert F#<i>, these days and tried the functional ways to meet the need. Object expression is one of my favorite feature of F#, and I just wonder whether there will be an OO way to express this idea. [;)] Best wishes Allen Lee

By on 6/27/2008 8:22 PM ()
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