why is base only possible in private members?

2019-07-15 09:04发布

问题:

I have some understanding of the difference between private members and let bindings. It may help me clarify my doubts understanding why something like this is not possible

type B () =
    inherit A ()

    let doSomething () =
        base.CallToA ()   

Is it to prevent partially constructed objects or some leaks during construction?

回答1:

The base keyword is only really needed to call a base-class implementation of a virtual method. That is the only case where you need base because you cannot invoke the method using the this instance (as that would refer to the override in the current class).

You are partially correct that the compiler wants to prevent you from accessing partially constructed objects though. However, this is done by requiring you to explicitly say that you want to be able to refer to the current instance inside the constructor using as this:

type B () as this =
  inherit A ()

  let doSomething () =
    this.CallToA ()   

The identifier this is just a name - similarly to member declarations - so you could use other name there.