Note that there are some explanatory texts on larger screens.

plurals
  1. POUnderstanding a change to protected/base member usage in F# 3.0
    text
    copied!<p>F# 3.0 adds stricter checks for calls to <code>base</code> and <code>protected</code> members. I have something like the following abstract class in C# that has <code>protected static</code> helper methods to be used by derived classes.</p> <pre class="lang-cs prettyprint-override"><code>public abstract class Processor { public abstract void Process(); protected static void Helper(object arg) { } } </code></pre> <p>In F#, one of those helper methods is passed as a first-class function:</p> <pre class="lang-ml prettyprint-override"><code>type DerivedProcessor() = inherit Processor() let init f = f () override x.Process() = init Processor.Helper </code></pre> <p>It compiles without complaint in 2.0, but in 3.0 produces an error:</p> <blockquote> <p>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.</p> </blockquote> <p>OK, it's easy enough to comply, just wrap the call in another static member </p> <pre class="lang-ml prettyprint-override"><code>static member private HelperWrapper(arg) = Processor.Helper(arg) </code></pre> <p>and pass that intead. But why?</p> <p>C# has no problem with this same pattern.</p> <pre class="lang-cs prettyprint-override"><code>public class HappyToCompile : Processor { private void Init(Action&lt;object&gt; f) { f(null); } public override void Process() { Init(Helper); } } </code></pre> <p><strong>The questions:</strong></p> <ol> <li>Why were the stricter checks added?</li> <li>(and related) What terrible problem does such a trivial workaround address?</li> <li>Is there a better design that this is supposed to encourage?</li> </ol>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload