Interface
Syntax
Similar to an abstract class but with only abstract members.
Atttribute:
[<AbstractClass>]attribute can not be used to declare an interface[<Interface>]attribute is optional but recommended in most cases
type [accessibility-modifier] interface-name =
abstract memberN : [ argument-typesN -> ] return-typeNInterface name begins with
Ito follow the .NET conventionArguments can be named (without parentheses otherwise π₯)
[<Interface>]
type IPrintable =
abstract member Print : format: string -> unitYou can also use verbose syntax with an interface ... end block.
β Not idiomatic except in the case of a member-less interface a.k.a marker interface.
type IMarker = interface endImplementation
2 ways of implementing an interface:
In an object expression π
In a type (as in Cβ―)
Default implementation
Fβ― 5.0 supports interfaces defining methods with default implementations written in Cβ― 8+ but does not allow them to be defined directly in Fβ―.
Fβ― interface is explicit
Fβ― interface implementation β‘ Explicit implementation of an interface in Cβ―
β Interface methods are accessible only by upcasting:
Implementing a generic interface
Inheritance
Defined with the inherit keyword
Last updated
Was this helpful?