Pages

Wednesday, February 15, 2012

Define property in F# interface

If you do the Azure programming with F#, you will bump into a problem where you have to define a property in an interface. Something like:

interface IMyInterface
{
        int MyProperty { get; set; }
}

The way to define this in F# is:

type IMyInterface= interface
    abstract MyProperty : int with get, set
end

The interesting thing about F# interface implementation is F# use explicit interface implementation. So if the interface definition does not have "set", then your class implementation will have to change if the class still need both "get" and "set".


type IMyInterface= interface
    abstract MyProperty : int with get
end

class implementation:

type MyClass() =
    interface IMyInterface with
       member this.MyProperty with get()  = ...
    member this.MyProperty with set(..) = ...


No comments: