Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>In F#, people don't use implementation inheritance (base classes) that often, so it would be useful to know what are you trying to do (e.g. more realistic example would be great). </p> <p>If I understand you correctly, the function <code>spread</code> should take a subtype of the type <code>Spreader</code> and return a value of the same type. You can write the signature of the function like this:</p> <pre><code>let foo&lt; 'T when 'T :&gt; Spreader &gt;(arg:'T) : 'T = // some implementation here </code></pre> <p>The type parameter <code>'T</code> will be some inherited class of <code>Spreader</code> and as the type signature shows, the function <code>foo</code> returns the same type as it takes as the argument.</p> <p>The implementation is a bit ugly, because you need to add a lot of dynamic type casts:</p> <pre><code>let foo&lt; 'T when 'T :&gt; Spreader &gt;(arg:'T) : 'T = match box arg with | :? Fire -&gt; (box (Fire())) :?&gt; 'T | :? Disease -&gt; (box (Disease())) :?&gt; 'T </code></pre> <p>Anyway, I agree with Brian that this seems like a scenario where discriminated unions would be a better choice. It would be great if you described your problem in some more details.</p> <hr> <p>Unlike the solution from Jon and <code>spread2</code> from Brian, the version above returns the same subtype as the one it gets as an argument, which means that the following should work:</p> <pre><code>let fire = new Fire() let fire2 = foo fire // 'fire2' has type 'Fire' fire2.FireSpecificThing() </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

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