- F# does not support the definition of
protected
methods. Here it is explained why
- F# replaces
virtual
methods with abstract
methods defined in abstract classes (see here).
I was wondering if there is a way to prevent access to abstract
methods from outside the derived classes at all.
Like Patryk Ćwiek, I also don't think it's possible, but here's one alternative:
From Design Patterns we know that we should favour Composition over Inheritance. In my experience, everything you can do with Inheritance, you can also do with Composition. As an example, you can always replace Template Method with a Strategy.
A Template Method is a typical use of an abstract method, but if you replace it with a Strategy, you can (sort of) hide it from clients:
type Foo(strategy : IBar) =
member this.CreateStuff() =
// 1. Do something concrete here
// 2. Use strategy for something here
// 3. Do something else concrete here
// 4. Return a result
No outside client of Foo
can invoke strategy
, so that accomplishes the same goal as making a member protected.
You may argue that the original creator of Foo
may keep a reference to strategy
, and will still be able to invoke it. That's true, but protected members aren't really completely hidden either, because you can often derive from the class in question, which enables you to invoke the protected member.
Another point is that if you separate the creator of Foo
from the client of Foo
, the strategy
will be unavailable to the client.