interfaces provide an abstract way of writing up the implementation details of a class. it is a template that declares the methods the class must implement and expose publicly.
syntax
an interface specifies the sets of related members that other classes implement. it has the following syntax −
// interface declaration:
[ attributes ]
type interface-name =
[ interface ]
[ inherit base-interface-name ...]
abstract member1 : [ argument-types1 -> ] return-type1
abstract member2 : [ argument-types2 -> ] return-type2
...
[ end ]
// implementing, inside a class type definition:
interface interface-name with
member self-identifier.member1 argument-list = method-body1
member self-identifier.member2 argument-list = method-body2
// implementing, by using an object expression:
[ attributes ]
let class-name (argument-list) =
{ new interface-name with
member self-identifier.member1 argument-list = method-body1
member self-identifier.member2 argument-list = method-body2
[ base-interface-definitions ]
}
member-list
please note −
in an interface declaration the members are not implemented.
the members are abstract, declared by the abstract keyword. however you may provide a default implementation using the default keyword.
you can implement interfaces either by using object expressions or by using class types.
in class or object implementation, you need to provide method bodies for abstract methods of the interface.
the keywords interface and end, which mark the start and end of the definition, are optional.
for example,
type iperson = abstract name : string abstract enter : unit -> unit abstract leave : unit -> unit
calling interface methods
interface methods are called through the interface, not through the instance of the class or type implementing interface. to call an interface method, you up cast to the interface type by using the :> operator (upcast operator).
for example,
(s :> iperson).enter() (s :> iperson).leave()
the following example illustrates the concept −
example
type iperson =
abstract name : string
abstract enter : unit -> unit
abstract leave : unit -> unit
type student(name : string, id : int) =
member this.id = id
interface iperson with
member this.name = name
member this.enter() = printfn "student entering premises!"
member this.leave() = printfn "student leaving premises!"
type stuffmember(name : string, id : int, salary : float) =
let mutable _salary = salary
member this.salary
with get() = _salary
and set(value) = _salary <- value
interface iperson with
member this.name = name
member this.enter() = printfn "stuff member entering premises!"
member this.leave() = printfn "stuff member leaving premises!"
let s = new student("zara", 1234)
let st = new stuffmember("rohit", 34, 50000.0)
(s :> iperson).enter()
(s :> iperson).leave()
(st :> iperson).enter()
(st :> iperson).leave()
when you compile and execute the program, it yields the following output −
student entering premises! student leaving premises! stuff member entering premises! stuff member leaving premises!
interface inheritance
interfaces can inherit from one or more base interfaces.
the following example shows the concept −
type interface1 =
abstract member doubleit: int -> int
type interface2 =
abstract member tripleit: int -> int
type interface3 =
inherit interface1
inherit interface2
abstract member printit: int -> string
type multiplierclass() =
interface interface3 with
member this.doubleit(a) = 2 * a
member this.tripleit(a) = 3 * a
member this.printit(a) = a.tostring()
let ml = multiplierclass()
printfn "%d" ((ml:>interface3).doubleit(5))
printfn "%d" ((ml:>interface3).tripleit(5))
printfn "%s" ((ml:>interface3).printit(5))
when you compile and execute the program, it yields the following output −
10 15 5