Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to generalize f# option?
    primarykey
    data
    text
    <p>I'm getting result of some calculations in form of <code>'a option when 'a :&gt; IBaseType</code>. There is tree of types derived from <code>IBaseType</code>, and I don't really know option of what particular type this one is, but the important thing is that it's option of specific derived, not base type. So I want to upcast it to <code>IBaseType option</code> to process it further. As option is generic type, it's impossible to do cast directly (in F#), and I have to do cast inside of Option.map. Nothing complicated, type inference works as expected... <img src="https://content.screencast.com/users/AndriyKvasnytsya/folders/Jing/media/2e93e2ab-2871-4b22-8747-284374f166d2/2012-11-13_2029.png" alt="inference ok"> intermediate casted option is also resolved as expected... <img src="https://content.screencast.com/users/AndriyKvasnytsya/folders/Jing/media/fd77a7cb-60db-41d8-828a-f77e15f27258/2012-11-13_2026.png" alt="intermediate inference ok"> until function is complete. At this point for some reason type inference decided that original option must be already of type <code>IBaseType option</code>: <img src="https://content.screencast.com/users/AndriyKvasnytsya/folders/Jing/media/50208452-1ef7-4886-b20d-5be206416a96/2012-11-13_2025.png" alt="inference not ok"></p> <p>Intermediate type was already resolved earlier, why did it decided to reassign infered type of the <code>op</code>? Sure this leads to runtime exceptions. Looks like compiler error, but the main rule is that there are no errors in compiler. </p> <p>So in the end it sounds really stupid: I'm out of ideas how to simply upcast simple option. Just to make picture clearer: <code>processResult</code> takes <code>IBaseType option</code> as argument. And here is source of troublesome function:</p> <pre><code>(fun (x: obj) -&gt; let op = x :?&gt; _ option let upcastOp = op |&gt; Option.map (fun y -&gt; y :&gt; IBaseType) upcastOp |&gt; processResult) </code></pre> <p>Any ideas how to deal with this?</p>
    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.
 

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