
{"id":4605,"date":"2019-12-16T00:39:42","date_gmt":"2019-12-16T05:39:42","guid":{"rendered":"https:\/\/ikriv.com\/blog\/?p=4605"},"modified":"2019-12-16T19:12:11","modified_gmt":"2019-12-17T00:12:11","slug":"the-evolution-of-nothing","status":"publish","type":"post","link":"https:\/\/ikriv.com\/blog\/?p=4605","title":{"rendered":"The evolution of nothing"},"content":{"rendered":"<p>In the beginning, Brian and Dennis created C, and C had the <code>void<\/code> keyword, and Brian and Dennis saw that this is good. It all went downhill from there.<\/p>\n<p><b>In C<\/b>, <code>void<\/code> is not a data type, it is a keyword that denotes absence of return value or input parameters. Another usage of <code>void<\/code> in C is to declare pointers to untyped data, as in <code>void*<\/code>. C does not really have a special value representing &#8220;nothing&#8221;. The NULL pointer is a pointer like any other, usually defined along the lines of <code>(void*)0L<\/code>.<\/p>\n<p><b>C++<\/b> pretty much inherited what C had, until C++11 defined <code>nullptr<\/code> value and <code>nullptr_t<\/code> type, which is a real data type. So, modern C++ has <code>void<\/code> keyword, <code>nullptr<\/code> value, and <code>nullptr_t<\/code> data type.<\/p>\n<p><b>C# and Java<\/b> have <code>void<\/code> keyword, and they have <code>null<\/code> value, but no special data type for that value.<\/p>\n<p><b>Javascript<\/b> does not have <code>void<\/code>, since it lacks type declarations in general, but to compensate, it has TWO empty values: <code>null<\/code> (no value) and <code>undefined<\/code> (like, really no value). This is a source of mild confusion, but can be ignored in most cases, e.g. <code>something==null<\/code> is true whether <code>something<\/code> is <code>null<\/code> or <code>undefined<\/code>.<\/p>\n<p><b>Python<\/b> has value <code>None<\/code> of type <code>NoneType<\/code>, and that&#8217;s pretty much it. Well, maybe that&#8217;s not so downhill.<\/p>\n<p><b>Typescript<\/b> outdoes them all. It has two empty values <code>null<\/code> and <code>undefined<\/code>, and 4 (four!) empty types: <code>never<\/code> (you ain&#8217;t gonna get a value), <code>null<\/code> (no value), <code>undefined<\/code> (like, really no value), and <code>void<\/code> (totally no value, I swear). There are also two language flavors: with strict null checks and without. <code>never<\/code> is a little like C&#8217;s <code>void<\/code>, one cannot declare a variable of type <code>never<\/code>. With strict null checks, <code>undefined<\/code> has one value <code>undefined<\/code>, <code>void<\/code> also has one value <code>undefined<\/code>, and <code>null<\/code> has one value <code>null<\/code>. Without strict null checks, all three can be either <code>null<\/code> or <code>undefined<\/code>. Oh yeah, and <code>typeof null<\/code> returns <code>\"object\"<\/code>, in any mode, just for laughs (and for compatibility with Javascript, I guess). This is <b>a lot<\/b> ado about nothing&#8230;<\/p>\n<p>PS. Did I mention you can combine types? The following happily compiles, even in strict null checks mode:<\/p>\n<pre>\/\/ Typescript\r\nfunction f() : void | undefined | null | never {\r\n   console.log(\"boo!\");\r\n}\r\n\r\nf();<\/pre>\n<p>I suppose, proper implementation of this function that uses all four possibilities, would be<\/p>\n<pre>function f(): void | undefined | null | never {\r\n   let random : number = Math.random();\r\n   console.log(random);\r\n   if (random < 0.25) return; \/\/ void\r\n   if (random < 0.5) return undefined;\r\n   if (random < 0.75) return null;\r\n   while (true); \/\/ never return\r\n}<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>In the beginning, Brian and Dennis created C, and C had the void keyword, and Brian and Dennis saw that this is good. It all went downhill from there. In <a href=\"https:\/\/ikriv.com\/blog\/?p=4605\" 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":[4],"tags":[],"class_list":["entry","author-ikriv","post-4605","post","type-post","status-publish","format-standard","category-hack"],"_links":{"self":[{"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/4605","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=4605"}],"version-history":[{"count":19,"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/4605\/revisions"}],"predecessor-version":[{"id":4624,"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/4605\/revisions\/4624"}],"wp:attachment":[{"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=4605"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=4605"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=4605"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}