Reputation: 2719
I have the following code with an interface and an abstract class.
type INode<'a> =
abstract member Parent:INode<'a>
abstract member Evaluate:unit->'a
abstract member Clone:INode<'a> -> INode<'a>
abstract member SelectRandom:int->INode<'a> option
[<AbstractClass>]
type TerminalNode<'a>() =
interface INode<'a> with
member this.SelectRandom indexOfCurrentNode =
let p = 1.0 / float indexOfCurrentNode
if Random().NextDouble() < p then Some (this:>INode<'a>) else None
I want to implement the hierarchy in this
but the compiler complains that all methods of the interface should be implemented.
What is the correct way to create an Abstract class that implements an interface and implements that interface's one method leaving other methods abstract?
Note: If you cannot see the image, your network is blocking imgur.com.
Upvotes: 1
Views: 88
Reputation: 36375
You can define the abstract members on the type, then provide the implementation which points at those abstract members. For example:
[<AbstractClass>]
type TerminalNode<'a>() as self =
abstract member Parent : INode<'a>
abstract member Evaluate : unit->'a
abstract member Clone:INode<'a> -> INode<'a>
interface INode<'a> with
member this.Parent = self.Parent
member this.Evaluate () = self.Evaluate ()
member this.Clone a = self.Clone a
member this.SelectRandom indexOfCurrentNode =
...
(note the as self
in the type
line, which lets you refer to the abstract type by the variable name self
)
Upvotes: 5