You can't implement "portions of an interface" in F#; for each class, each interface is "all or nothing".

That said, you can kinda hack this by defining individual members, concrete or abstract, in the BaseClass and then forwarding the interface to those members, along these lines:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
type ISomething =
        abstract OneMethod : string -> bool
        abstract AnotherMethod : string -> bool
        abstract OneProp: decimal with get, set

[<AbstractClass>]
type BaseClass(c:decimal) as this =
        let mutable foo = c
        member x.OneMethod (str:string) = str.Length > 0
        member x.OneProp with get() = foo and set(v) = foo <- v
        abstract AnotherMethod : string -> bool
        interface ISomething with
                member x.OneMethod str = this.OneMethod(str)
                member x.OneProp with get() = this.OneProp and set(v) = this.OneProp <- v
                member x.AnotherMethod str = this.AnotherMethod(str)

type ConcreteType(c:decimal) as this =
        inherit BaseClass(c)
        override x.AnotherMethod str = str.Length > 20

let c = new ConcreteType(new decimal(10))
let i = c :> ISomething
printfn "%A" (i.AnotherMethod "foo")
By on 10/21/2008 4:47 PM ()

Thank you very much for your reply, as it actually clarifies this issue I've been bagning my head on. I believe it's a pity for such an interesting language to lack the feature of partial interface implementations as it can come handy in base classes (such as this one). Another way I could hack this would be to make the base class throw a NotImplementedException on the interface members I don't want to implement there, but still the type would be concrete and I wished there was a way around this. Who knows, maybe in future versions of the language this thing will be considered. Finally, as a sidenote, I find it pretty lame to have to use attrivutes for things such as abstract classes or overloads...I understand the principle of keeping the compiler simple and the principle of limiting the amount of keywords, but pushing it this far seems a bit excessive to me.

Thanks again and best regards

By on 10/22/2008 5:46 PM ()

Hi there

We plan to support partially implemented interfaces in a future version of F#

Thanks for your other comments - we are grateful for the feedback.

Kind regards

don

By on 10/24/2008 3:11 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