
{"id":25,"date":"2008-06-22T14:21:05","date_gmt":"2008-06-22T19:21:05","guid":{"rendered":"http:\/\/ikriv.com:8765\/blog\/?p=25"},"modified":"2008-06-22T14:21:05","modified_gmt":"2008-06-22T19:21:05","slug":"f-types-classes-and-nullability","status":"publish","type":"post","link":"https:\/\/ikriv.com\/blog\/?p=25","title":{"rendered":"F#: Types, Classes and Nullability"},"content":{"rendered":"<p>F# has two kinds of data types &#8211; &#8220;classes&#8221; and &#8220;types&#8221;. Types are bags of bits, similar to C structs, only immutable (that&#8217;s C, without sharp). Classes are classes in a normal object-oriented sense.<\/p>\n<p>F# types cannot be <code>null<\/code>. Values that may or may not be present are expressed via special type <code>'a option<\/code>. So, <code>int option<\/code> means optional integer, conceptually very similar to <code>Nullable&lt;int&gt;<\/code>. <code>int option<\/code> can take values <code>None<\/code> or <code>Some(x)<\/code>.<br \/>\n<!--more--><br \/>\nIn Ocaml classes are also not nullable. F# needed to be compatible with .NET libraries, so it introduced <code>null<\/code> keyword, but only for classes. F# classes are both nullable and optionable.<\/p>\n<p>Note, that <code>null<\/code> is not the same as <code>None<\/code>, and <code>Nullable&lt;int&gt;<\/code> and <code>int option<\/code> are different data types.<\/p>\n<p>Confused? I bet you are. And I have one more thing up my sleeve: .NET structs. This is a nut that F# authors did not crack yet.<\/p>\n<p>F# types are .NET structs are both not-nullable, but they are not the same thing. F# types can&#8217;t have methods, .NET structs can. F# types don&#8217;t derive from <code>object<\/code>, .NET structs do. Et cetera, et cetera.<\/p>\n<p>Also, while <code>Nullable&lt;T&gt;<\/code> is conceptually similar to <code>T option<\/code>, these are two different, incompatible things.<\/p>\n<p>The bottom line is, checking something for <code>null<\/code> in F# is very not obvious.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>F# has two kinds of data types &#8211; &#8220;classes&#8221; and &#8220;types&#8221;. Types are bags of bits, similar to C structs, only immutable (that&#8217;s C, without sharp). Classes are classes in <a href=\"https:\/\/ikriv.com\/blog\/?p=25\" 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-25","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\/25","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=25"}],"version-history":[{"count":0,"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/25\/revisions"}],"wp:attachment":[{"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=25"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=25"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=25"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}