tags:

views:

115

answers:

2

incorporating leppies feedback it compiles - but IMO some drawbacks I want each sub class to be forced by the compiler to define their own Uri property. Code as it is now:

[<AbstractClass>] 
type UriUserControl() = 
    inherit UserControl()
    interface IUriProvider with 
        member this.Uri with get() = null

Interesting enough, the class I defines which inerits from above does not show a public Uri property:

type Page2() as this =
    inherit UriUserControl()
    let uriStr = "/FSSilverlightApp;component/Page2.xaml"
    let mutable uri = new System.Uri(uriStr, System.UriKind.Relative)
    do
        Application.LoadComponent(this, uri)

    member public this.Uri with get () = uri

I would like to define an abstract class that inherits from UserControl and my own Interface IUriProvider, but doesn't implement it. The goal is to be able to define pages (for silverlight) that implement UserControl but also provide their own Uri's (and then stick them in a list / array and deal with them as a set:

type IUriProvider = 
interface
    abstract member uriString: String ;
    abstract member Uri : unit -> System.Uri ;
end

[<AbstractClass>] 
type UriUserControl() as this = 
    inherit IUriProvider with
        abstract member uriString: String ;
    inherit UserControl()

Also the Uri in the definition - I would like to implement as a property getter - and am having issues with that as well.

this does not compile

type IUriProvider = 
    interface
        abstract member uriString: String with get;
end

Thank you...

A: 

From a .NET point of view, you would need to at least provide an abstract implementation for the interface. But that again could proof problematic due to default interface accessibility, which would require some more glue again for an explicit implementation.

leppie
+3  A: 

Here is a way to do it:

type IUriProvider =  
    abstract member UriString: string
    abstract member Uri : System.Uri

[<AbstractClass>]  
type UriUserControl() as this =  
    inherit System.Windows.Controls.UserControl() 
    abstract member Uri : System.Uri
    abstract member UriString : string
    interface IUriProvider with 
        member x.Uri = this.Uri
        member x.UriString = this.UriString

Note that you have to provide an implementation of the interface (since all interface implementations in F# are explicit), but this can just refer back to abstract members in the class. Then you can subclass thusly:

type ConcreteUriUserControl() =
    inherit UriUserControl()
    override this.Uri = null
    override this.UriString = "foo"
Brian