{"id":16524,"date":"2016-12-02T14:24:54","date_gmt":"2016-12-02T19:24:54","guid":{"rendered":"http:\/\/mjtsai.com\/blog\/?p=16524"},"modified":"2016-12-02T14:24:54","modified_gmt":"2016-12-02T19:24:54","slug":"the-rawrepresentable-protocol-in-swift","status":"publish","type":"post","link":"https:\/\/mjtsai.com\/blog\/2016\/12\/02\/the-rawrepresentable-protocol-in-swift\/","title":{"rendered":"The RawRepresentable Protocol in Swift"},"content":{"rendered":"<p><a href=\"https:\/\/oleb.net\/blog\/2016\/11\/rawrepresentable\/\">Ole Begemann<\/a>:<\/p>\n<blockquote cite=\"https:\/\/oleb.net\/blog\/2016\/11\/rawrepresentable\/\">\n<p>Note that adding raw values to an enum doesn&rsquo;t affect how the enum <a href=\"https:\/\/github.com\/apple\/swift\/blob\/master\/docs\/ABI.rst#fragile-enum-layout\">is laid out in memory<\/a>. The compiler always determines how many bits it needs to discriminate between all enum cases and then assigns a unique integer tag value to each case. Even for enums with integer raw values, this tag is not the same as the raw value &mdash; they are completely different things. This also means you don&rsquo;t have to worry that an enum with strings as raw values will take up more memory than a &ldquo;plain&rdquo; enum &mdash; the constant strings are only stored once in the binary, not for each instance.<\/p>\n<p>[&#8230;]<\/p>\n<p>In fact, the raw value syntax is simply shorthand for conforming the enum to the <a href=\"https:\/\/developer.apple.com\/reference\/swift\/rawrepresentable\"><code>RawRepresentable<\/code><\/a> protocol, which defines the <code>rawValue<\/code> property and initializer. When you define the above enum, the compiler generates code equivalent to this[&#8230;]<\/p>\n<p>[&#8230;]<\/p>\n<p>Once you realize that there&rsquo;s no magic behind enums with raw values, it opens up the possibility to use all kinds of types as raw values.<\/p>\n<\/blockquote>","protected":false},"excerpt":{"rendered":"<p>Ole Begemann: Note that adding raw values to an enum doesn&rsquo;t affect how the enum is laid out in memory. The compiler always determines how many bits it needs to discriminate between all enum cases and then assigns a unique integer tag value to each case. Even for enums with integer raw values, this tag [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"apple_news_api_created_at":"","apple_news_api_id":"","apple_news_api_modified_at":"","apple_news_api_revision":"","apple_news_api_share_url":"","apple_news_coverimage":0,"apple_news_coverimage_caption":"","apple_news_is_hidden":false,"apple_news_is_paid":false,"apple_news_is_preview":false,"apple_news_is_sponsored":false,"apple_news_maturity_rating":"","apple_news_metadata":"\"\"","apple_news_pullquote":"","apple_news_pullquote_position":"","apple_news_slug":"","apple_news_sections":"\"\"","apple_news_suppress_video_url":false,"apple_news_use_image_component":false,"footnotes":""},"categories":[4],"tags":[255,46,71,901],"class_list":["post-16524","post","type-post","status-publish","format-standard","hentry","category-programming-category","tag-compiler","tag-languagedesign","tag-programming","tag-swift-programming-language"],"apple_news_notices":[],"_links":{"self":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/16524","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/comments?post=16524"}],"version-history":[{"count":1,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/16524\/revisions"}],"predecessor-version":[{"id":16525,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/16524\/revisions\/16525"}],"wp:attachment":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/media?parent=16524"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/categories?post=16524"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/tags?post=16524"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}