{"id":1841,"date":"2012-10-15T20:00:00","date_gmt":"2012-10-15T19:00:00","guid":{"rendered":"https:\/\/www.reenigne.org\/blog\/?p=1841"},"modified":"2012-09-23T09:17:16","modified_gmt":"2012-09-23T08:17:16","slug":"sum-types-in-alfe","status":"publish","type":"post","link":"https:\/\/www.reenigne.org\/blog\/sum-types-in-alfe\/","title":{"rendered":"Sum types 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 | Bar<\/strong> is a \"sum\" type, in other words a type that can have any values that <strong>Foo<\/strong> can as well as any values that <strong>Bar<\/strong> can. Also known as a tagged union or an algebraic data type.<\/p>\n<p><strong>Foo | Bar<\/strong> acts like a base class (supertype) of both <strong>Foo<\/strong> and <strong>Bar<\/strong>, since either of the latter are Liskov-substitutable for the former, and the downcast required to convert from the former to the latter is identical that required for casting from a base class. <strong>Foo | Baz<\/strong> also acts like a (completely separate) base class of <strong>Foo<\/strong>, so sum types provide something <a href=\"http:\/\/en.wikipedia.org\/wiki\/Phrases_from_The_Hitchhiker's_Guide_to_the_Galaxy\">almost, but not quite, entirely unlike<\/a> multiple inheritance.<\/p>\n<p><strong>Foo | Bar<\/strong> has all the methods and members of the most derived common base class of <strong>Foo<\/strong> and <strong>Bar<\/strong> (and, if that common base class has no other subclasses not derived from <strong>Foo<\/strong> or <strong>Bar<\/strong>, it'll be identical to <strong>Foo | Bar<\/strong>. In particular, <strong>Foo | Foo<\/strong> is identical to <strong>Foo<\/strong>).<\/p>\n<p><strong>Foo | Bar<\/strong> is syntactic sugar for a special template <strong>Either&lt;Foo, Bar&gt;<\/strong>. Similarly, <strong>Foo | Bar | Baz<\/strong> is syntactic sugar for <strong>Either&lt;Foo, Bar, Baz&gt;<\/strong> - <strong>Either<\/strong> is a variadic template. <strong>Either&lt;Foo&gt;<\/strong> is just another name for <strong>Foo<\/strong>.<\/p>\n<p>How <strong>Either<\/strong> is actually implemented is up to the compiler, but some kind of boxing will probably be necessary in the case when <strong>Foo<\/strong> and <strong>Bar<\/strong> are not both pointers to types with vtable pointers.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>This is part of the ALFE types series. Foo | Bar is a \"sum\" type, in other words a type that can have any values that Foo can as well as any values that Bar can. Also known as a tagged union or an algebraic data type. Foo | Bar acts like a base class [&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-1841","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\/1841","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=1841"}],"version-history":[{"count":6,"href":"https:\/\/www.reenigne.org\/blog\/wp-json\/wp\/v2\/posts\/1841\/revisions"}],"predecessor-version":[{"id":1843,"href":"https:\/\/www.reenigne.org\/blog\/wp-json\/wp\/v2\/posts\/1841\/revisions\/1843"}],"wp:attachment":[{"href":"https:\/\/www.reenigne.org\/blog\/wp-json\/wp\/v2\/media?parent=1841"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.reenigne.org\/blog\/wp-json\/wp\/v2\/categories?post=1841"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.reenigne.org\/blog\/wp-json\/wp\/v2\/tags?post=1841"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}