Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>The <code>protected</code> modifier can be quite problematic in F#, because you often need to call members from a lambda expression. However, when you do that, you no longer access the method from within the class. This also causes confusion when using protected members declared in C# (see for example <a href="https://stackoverflow.com/questions/2366490/f-how-to-access-protected-member">this SO question</a>). If you could declare a <code>protected</code> member, the following code could be surprising: </p> <pre><code>type Base() = protected member x.Test(a) = a &gt; 10 type Inherited() = inherit Base() member x.Filter(list) = list |&gt; List.filter (fun a -&gt; x.Test(a)) </code></pre> <p>This code wouldn't work, because you're calling <code>Test</code> from a lambda function (which is a different object than the current instance of <code>Test</code>), so the code wouldn't work. I think this is tha main reason for not supporting the <code>protected</code> modifier in F#.</p> <p>In F# you typically use implementation inheritance (that is, inheriting from a base class) much less frequently than in C#, so you shouldn't need <code>protected</code> as often. Instead, it is usually preferred to use interfaces (in the object-oriented F# code) and higher-order functions (in the functional code). However, it is difficult to say how to avoid the need for <code>protected</code> in general (other than by avoiding implementation inheritance). Do you have some specific example which motivated your question?</p>
 

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