{"id":1829,"date":"2012-10-13T20:00:31","date_gmt":"2012-10-13T19:00:31","guid":{"rendered":"https:\/\/www.reenigne.org\/blog\/?p=1829"},"modified":"2012-09-23T09:17:38","modified_gmt":"2012-09-23T08:17:38","slug":"pointers-in-alfe","status":"publish","type":"post","link":"https:\/\/www.reenigne.org\/blog\/pointers-in-alfe\/","title":{"rendered":"Pointers in ALFE"},"content":{"rendered":"<p>This is part of the <a href=\"https:\/\/www.reenigne.org\/blog\/the-alfe-type-system\">ALFE types series<\/a>.<\/p>\n<p>As a high-performance language with C++ heritage, it should not be at all surprising that ALFE has pointers.<\/p>\n<p>Given a type <strong>Foo<\/strong>, the type of a pointer that points to values of that type is <strong>Foo*<\/strong>.<\/p>\n<p>In C and C++, the grammar binds the asterisk to the variable name rather than the type. So (particularly in C) declarations of pointers look like this:<\/p>\n<pre lang=\"c\">Foo *foo;<\/pre>\n<p>That was very confusing to me when I first started learning C. Once I thought of it as:<\/p>\n<pre lang=\"c\">Foo* foo;<\/pre>\n<p>instead, the whole thing made much more sense. In C++, this style is idiomatic, due to <a href=\"http:\/\/www.stroustrup.com\/bs_faq2.html#whitespace\">C++&#8217;s emphasis on types over expressions<\/a>. However, the grammar hasn&#8217;t caught up so:<\/p>\n<pre lang=\"c\">Foo* foo, bar;<\/pre>\n<p>declares a Foo* called foo and a Foo called bar.<\/p>\n<p>ALFE fixes this once and for all:<\/p>\n<pre lang=\"c\">Foo* foo;<\/pre>\n<p>declares a real <strong>Foo*<\/strong> called <strong>foo<\/strong> rather than an imaginary Foo called *foo.<\/p>\n<p>The second difference between ALFE and C++ is that pointers are by default not nullable. In other words, <strong>Foo*<\/strong> carries the connotation of pointing to an actual <strong>Foo<\/strong>, so null checking is not required (or even possible, since comparing a <strong>Foo*<\/strong> to <strong>Null<\/strong> will always yield <strong>false<\/strong>). In other words, ALFE fixes <a href=\"http:\/\/qconlondon.com\/london-2009\/presentation\/Null+References:+The+Billion+Dollar+Mistake\">Tony Hoare&#8217;s billion dollar mistake<\/a>. Put like that, it&#8217;s a no-brainer.<\/p>\n<p>Of course, in practice there will be such things as an uninitialized <strong>Foo*<\/strong>, one that points to a <strong>Foo<\/strong> that went out of scope or one that points to one past the end of an array. However, those aren&#8217;t things that could be checked for by a function which just takes a <strong>Foo*<\/strong> as an argument &#8211; if the pointer might be invalid there would have to be an out of band signalling mechanism saying whether it is safe to dereference that pointer or not. I&#8217;ll be talking about an ideally suited mechanism in a <a href=\"https:\/\/www.reenigne.org\/blog\/maybes-in-alfe\">future post in this series<\/a>.<\/p>\n<p><strong>Foo*<\/strong> is syntactic sugar for the template instantiation <strong>Pointer&lt;Foo&gt;<\/strong>. This is mostly to simplify the implementation of the compiler, but it does also mean that the type constructor <strong>Pointer<\/strong> could be passed to some template that has a parameter of kind <strong>&lt;&gt;<\/strong>. Not sure why you might want to do such a thing, but it&#8217;s there is somebody does find a use for it.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>This is part of the ALFE types series. As a high-performance language with C++ heritage, it should not be at all surprising that ALFE has pointers. Given a type Foo, the type of a pointer that points to values of that type is Foo*. In C and C++, the grammar binds the asterisk to the [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4,27],"tags":[],"class_list":["post-1829","post","type-post","status-publish","format-standard","hentry","category-computer","category-language"],"_links":{"self":[{"href":"https:\/\/www.reenigne.org\/blog\/wp-json\/wp\/v2\/posts\/1829","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.reenigne.org\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.reenigne.org\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.reenigne.org\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.reenigne.org\/blog\/wp-json\/wp\/v2\/comments?post=1829"}],"version-history":[{"count":7,"href":"https:\/\/www.reenigne.org\/blog\/wp-json\/wp\/v2\/posts\/1829\/revisions"}],"predecessor-version":[{"id":1832,"href":"https:\/\/www.reenigne.org\/blog\/wp-json\/wp\/v2\/posts\/1829\/revisions\/1832"}],"wp:attachment":[{"href":"https:\/\/www.reenigne.org\/blog\/wp-json\/wp\/v2\/media?parent=1829"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.reenigne.org\/blog\/wp-json\/wp\/v2\/categories?post=1829"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.reenigne.org\/blog\/wp-json\/wp\/v2\/tags?post=1829"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}