
{"id":26,"date":"2008-06-29T21:28:15","date_gmt":"2008-06-30T02:28:15","guid":{"rendered":"http:\/\/ikriv.com:8765\/blog\/?p=26"},"modified":"2008-06-29T21:28:15","modified_gmt":"2008-06-30T02:28:15","slug":"f-type-casts","status":"publish","type":"post","link":"https:\/\/ikriv.com\/blog\/?p=26","title":{"rendered":"F# Type Casts"},"content":{"rendered":"<p>Typecast matters are complicated in F#. Unlike many other languages, F# does not perform implicit upcasts by default. E.g. if class <code>Derived<\/code> derives from <code>Base<\/code>, and we have<\/p>\n<pre><code>let func( b : Base ) = ...\nlet d : Derived  = ...\n<\/code><\/pre>\n<p>then <code>func(d)<\/code> will not work. <!--more-->The compiler will complain that type <code>Derived<\/code> is not compatible with <code>Base<\/code>. This comes as a shock to an object-oriented developer. We must explicitly &#8220;upcast&#8221; derived class to base class like this: <code>func (upcast d)<\/code> or <code>func (d :&gt; Base)<\/code>. In the former case, F# automatically figures out the destination type, in the latter case we explicitly say what it is.<\/p>\n<p>Alternatively, we can explicitly declare <code>f<\/code> as accepting a polymorphic argument:<\/p>\n<p><code>let func( b : <b>#<\/b>Base ) = ...<\/code><\/p>\n<p>I believe that internally this it implemented via generics. with actual argument type becoming a template parameter.<\/p>\n<p>Overall, F# introduces five typecast operators:<\/p>\n<table border=\"1\">\n<tr>\n<th>Operation<\/th>\n<th>F#<\/th>\n<th>C#<\/th>\n<th>Explanation<\/th>\n<\/tr>\n<tr>\n<td><code>upcast<\/code><\/td>\n<td><code>upcast x<\/code><\/td>\n<td><code>x<\/code><\/td>\n<td>Automatically deduce destination type T from context and cast x to T. Static type of x must be a subtype of T, or the code will not compile.<\/td>\n<\/tr>\n<tr>\n<td><code>:&gt;<\/code><\/td>\n<td><code>x :&gt; T<\/code><\/td>\n<td><code>(T)x<\/code><\/td>\n<td>Cast x to T. Static type of x must be a subtype of T, or the code will not compile.<\/td>\n<\/tr>\n<tr>\n<td><code>downcast<\/code><\/td>\n<td><code>downcast x<\/code><\/td>\n<td>no equivalent<\/td>\n<td>Automatically deduce destination type T from context and cast x to T. Static type of x must be a base class of T, or the code will not compile.<\/td>\n<\/tr>\n<tr>\n<td><code>:?&gt;<\/code><\/td>\n<td><code>x :?&gt; T<\/code><\/td>\n<td><code>x as T<\/code><\/td>\n<td>If dynamic type of x is T or is derived from T, returns (T)x. Otherwise returns null, even if T is not a class type. Static type of x must be a base class of T, otherwise compilation error occurs.<\/td>\n<\/tr>\n<tr>\n<td><code>:?&nbsp;<\/code><\/td>\n<td> <code>if x ?: T then<\/code><\/td>\n<td><code>if (x is T)<\/code><\/td>\n<td>Returns true if dynamic type of x is T or is derived from T, otherwise returns false. Static type of x must be a base class of T, or the code will not compile.<\/td>\n<\/tr>\n<\/table>\n<p>In addition to the typecast operators, F# uses pattern matching operator <code>?: [as variable]<\/code>, e.g.<\/p>\n<pre><code>let func (x:Base) =\n match x with\n  | :?&nbsp;TwiceDerived as td -> td_func td\n  | :?&nbsp;Derived -> 10\n  | _ -> 0\n<\/code><\/pre>\n<p>Some notes:<br \/>\n* There are too many cast operators, and their spelling are quite confusing. I am afraid not many people can intuitively tell what a <code>:?&gt;<\/code> might mean.<br \/>\n* Polymorphism is applied inconsistently. E.g. it does not work in function calls by default, but it does work in the <code>:?&nbsp;<\/code>operator. In the above example, if actual type of <code>x<\/code> is <code>DerivedFromDerived<\/code>, <code>:?&nbsp;Derived<\/code> will still work<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Typecast matters are complicated in F#. Unlike many other languages, F# does not perform implicit upcasts by default. E.g. if class Derived derives from Base, and we have let func( <a href=\"https:\/\/ikriv.com\/blog\/?p=26\" class=\"more-link\">[&hellip;]<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"Layout":"","footnotes":""},"categories":[10],"tags":[9],"class_list":["entry","author-ikriv","has-more-link","post-26","post","type-post","status-publish","format-standard","category-fsharp","tag-fsharp"],"_links":{"self":[{"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/26","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=26"}],"version-history":[{"count":0,"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/26\/revisions"}],"wp:attachment":[{"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=26"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=26"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=26"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}