title: F# Access inherited type's field
body: <p>Given that i have a base type that is defined like this:</p>
  
  <pre><code>[&lt;AbstractClass&gt;]
  type HookBase(hookType: HookId, threadId: int) =
      let mutable hHook = IntPtr.Zero
      ...
  
      member this.SetHook() =
          // Set the hook using SetWindowsHookEx(...)
          ...
          hHook &lt;- hhk
  
      member this.Unhook() =
          // Remove the hook
          hHook &lt;- IntPtr.Zero
  </code></pre>
  
  <p>and a derived type like this:</p>
  
  <pre><code>type ForegroundIdleHook(thread: uint32) as this =
      inherit HookBase(HookId.WH_FOREGROUNDIDLE, int thread)
  
      let hookCallback nCode wParam lParam =
          ...
          CallNextHookEx((* here has to be the hook's handle *), nCode, wParam, lParam)
  
      ...
  </code></pre>
  
  <p>Since i don't want to have the <code>SetHook()</code> and <code>Unhook()</code> code in every
  type (in the end there will be 15 hook types), i thought of using the base type. But F# does not support <code>protected</code> members, i now have the problem to get the <code>hHook</code> within the derived types and set the callback in the base type.</p>
  
  <p>I thought of using the <a href="https://msdn.microsoft.com/en-us/library/system.runtime.compilerservices.callermembernameattribute(v=vs.110).aspx" rel="nofollow noreferrer">CallerMemberNameAttribute</a> and a predefined method to achiev this:</p>
  
  <pre><code>// Base type
  member this.SetCallback(callback: HookProc, [&lt;CallerMemberName&gt;] ?name: string) =
      ...
  
  member this.GetHHook([&lt;CallerMemberName&gt;] ?name: string) =
      ...
  
  // Derived type
  do
      this.SetCallback(this.Proc)
  
  member private this.Proc = HookProc(hookCallback)
  </code></pre>
  
  <p>In case of <code>SetCallback</code> a counter could be established to ensure that there is only one invocation of the method, but that won't work for <code>GetHHook</code> because the hook could be set and unset several times during the instances' lifetime.</p>
  
  <p>Additionally due to the fact that other callees also could have the same name as the desired method this way isn't foolprove nor would i like to have a public scope for these methods either.
  Beside in a test of mine it seems the attribute does not work with F# because one has to provide an explicit default value for the parameter. (Am i missing something here?)</p>
  
  <p>Is there a better way to get the hook's handle and set the callback and not to have a base type have said methods 15 times?</p>
  
  <p><b>Edit:</b> As indicated <a href="https://stackoverflow.com/questions/14117842/is-it-possible-to-use-callermembernameattribute-in-f">here</a> the <code>CallerMemberName</code> attribute is not supported by F#, so the option using it is inapplicable.</p>