F# 3.0 adds stricter checks for calls to base
and protected
members. I have something like the following abstract class in C# that has protected static
helper methods to be used by derived classes.
public abstract class Processor {
public abstract void Process();
protected static void Helper(object arg) { }
}
In F#, one of those helper methods is passed as a first-class function:
type DerivedProcessor() =
inherit Processor()
let init f =
f ()
override x.Process() =
init Processor.Helper
It compiles without complaint in 2.0, but in 3.0 produces an error:
A protected member is called or 'base' is being used. This is only allowed in the direct implementation of members since they could escape their object scope.
OK, it's easy enough to comply, just wrap the call in another static member
static member private HelperWrapper(arg) = Processor.Helper(arg)
and pass that intead. But why?
C# has no problem with this same pattern.
public class HappyToCompile : Processor {
private void Init(Action<object> f) {
f(null);
}
public override void Process() {
Init(Helper);
}
}
The questions:
- Why were the stricter checks added?
- (and related) What terrible problem does such a trivial workaround address?
- Is there a better design that this is supposed to encourage?