BioInfoBrett
BioInfoBrett

Reputation: 305

How to specify a self return type

I've got an abstract class called "RecordA" with some slicing logic from which I want to subclass a couple of classes. I want "Record" to define an Abstract Slice method that must be implemented to return it's own type, e. g. type "RecordB" when sliced returns the RecordB type.

Is this possible in Fsharp? If not, why not?

Upvotes: 2

Views: 572

Answers (1)

Tarmil
Tarmil

Reputation: 11362

The only way for an interface to have a method that returns a different type depending on the concrete class is to make it parametric:

type Record<'Concrete> =
    abstract member Slice : unit -> 'Concrete

type RecordB() =
    interface Record<RecordB> with
        member this.Slice() = new RecordB()

Upvotes: 4

Related Questions