{"id":1857,"date":"2012-10-19T20:00:01","date_gmt":"2012-10-19T19:00:01","guid":{"rendered":"https:\/\/www.reenigne.org\/blog\/?p=1857"},"modified":"2012-09-23T09:16:16","modified_gmt":"2012-09-23T08:16:16","slug":"fixed-length-arrays-in-alfe","status":"publish","type":"post","link":"https:\/\/www.reenigne.org\/blog\/fixed-length-arrays-in-alfe\/","title":{"rendered":"Fixed-length arrays 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><strong>Foo[2]<\/strong>, <strong>Foo[3]<\/strong>, <strong>Foo[4]<\/strong> etc. are arrays whose length is fixed at compile time (not to be confused with <strong>[Foo]<\/strong> whose length is unknown and possibly undefined even at runtime). Passing around a value of array type copies the entire array (arrays don't decay to pointers). Operations defined on the elements can also be applied (element-wise) to the array itself, so you can write things like:<\/p>\n<pre lang=\"c\">Int[2] centre(Int[2] topLeft, Int[2] bottomRight)\r\n{\r\n    return (topLeft + bottomRight)\/2;\r\n}<\/pre>\n<p><strong>Foo[2]<\/strong> also has a template form, but since template arguments can only be types and not integers like they can in C++, there is a twist: <strong>Array&lt;Foo, Class {Int n=2;}&gt;<\/strong>. Any type can be used for the second argument as long as it has a public member named <strong>n<\/strong> of type <strong>Int<\/strong> with value known at compile time. This allows templates to work generically with arrays of different lengths.<\/p>\n<p><strong>Foo[n]<\/strong> can be indexed by an integer to yield an (RValue or LValue) <strong>Foo<\/strong>. It can also be indexed by a sequence (whose values are known at run time) to yield a slice of the array.<\/p>\n<p>An array can be coerced to a sequence, and the compiler will box up the element count and pointer as necessary.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>This is part of the ALFE types series. Foo[2], Foo[3], Foo[4] etc. are arrays whose length is fixed at compile time (not to be confused with [Foo] whose length is unknown and possibly undefined even at runtime). Passing around a value of array type copies the entire array (arrays don't decay to pointers). Operations defined [&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-1857","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\/1857","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=1857"}],"version-history":[{"count":8,"href":"https:\/\/www.reenigne.org\/blog\/wp-json\/wp\/v2\/posts\/1857\/revisions"}],"predecessor-version":[{"id":1895,"href":"https:\/\/www.reenigne.org\/blog\/wp-json\/wp\/v2\/posts\/1857\/revisions\/1895"}],"wp:attachment":[{"href":"https:\/\/www.reenigne.org\/blog\/wp-json\/wp\/v2\/media?parent=1857"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.reenigne.org\/blog\/wp-json\/wp\/v2\/categories?post=1857"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.reenigne.org\/blog\/wp-json\/wp\/v2\/tags?post=1857"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}