What is the correct way to handle this situation. I have one method in my F# class DogTree that should fulfill the requirement of implementing a Bark() method for both interfaces.
type ITree =
interface
abstract Bark : unit -> unit
abstract Grow : unit -> unit
end
type IDog =
interface
abstract Bark : unit -> unit
abstract ChaseCar : unit -> unit
end
type TreeDog =
// so the "and" syntax below doesn't work - what is the correct way to handle?
interface IDog and ITree with
member this.Bark() = printfn "Bark"
You can delegate to a common implementation:
Or, more appropriately:
(Note that this defines an extra method in the class named
Bark, that is used as the implementation for both interfaces.)If you declare a primary constructor in your class, you can use this instead: