I have tried to call an abstract function from a method's own implementation.

[<AbstractClass>]
type CallAnAbstract () = 

    let doSomethingElse () = printfn "Done Something.."

    abstract doSomethingAbstract: unit -> unit

    member this.DoSomething =
        printfn "Done Something.."
        doSomethingElse ()
        doSomethingAbstract ()

which results in the error:

error FS0039: The value or constructor 'doSomethingAbstract' is not defined

Is calling an abstract function from a method's own implementation illegal or does the problem lie somewhere else?

If that being illegal is the case, here (see answer) @Mark Seemann explains composition over inheritance:

[<AbstractClass>]
type UseComposition (doSomethingElseDefinedLater) = 

    let doSomethingElse () = printfn "Done Something.."

    member this.DoSomething =
        printfn "Done Something.."
        doSomethingElse ()
        doSomethingElseDefinedLater ()

What are the alternatives? I'd avoid passing the function via constructor and I'd like to reuse the some code, having DoSomething have "some implementation" until the call to the abstract function.

有帮助吗?

解决方案

You need to use a this reference:

this.doSomethingAbstract()
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top