{"id":19192,"date":"2017-10-11T15:06:53","date_gmt":"2017-10-11T19:06:53","guid":{"rendered":"https:\/\/mjtsai.com\/blog\/?p=19192"},"modified":"2017-10-11T15:06:53","modified_gmt":"2017-10-11T19:06:53","slug":"hashvisitable-swift-evolution-proposal","status":"publish","type":"post","link":"https:\/\/mjtsai.com\/blog\/2017\/10\/11\/hashvisitable-swift-evolution-proposal\/","title":{"rendered":"HashVisitable Swift Evolution Proposal"},"content":{"rendered":"<p><a href=\"https:\/\/blog.definiteloops.com\/ha-r-sh-visitors-8c0c3686a46f\">Vincent Esche<\/a> (via <a href=\"https:\/\/twitter.com\/smileyborg\/status\/841530829046128641\">Tyler Fox<\/a>):<\/p>\n<blockquote cite=\"https:\/\/blog.definiteloops.com\/ha-r-sh-visitors-8c0c3686a46f\"><p>In short: <code>Hashable<\/code> is utterly error-prone and does not compose well. And that&rsquo;s a shame!<\/p><p>[&#8230;]<\/p><p>If even the official Swift documentation gets the implementation of <code>hashValue<\/code> wrong, then who is to expect the average Swift programmer to do any better?<\/p><p>[&#8230;]<\/p><p>Now, I&rsquo;m the first to admit that a scenario where one needs to have multiple hashing algorithms is rather rare.<\/p><p>There are however certain circumstances where multiple hashes per value are desired, if not outright necessary: <a href=\"https:\/\/en.wikipedia.org\/wiki\/Bloom_filter\">Bloom Filters<\/a>.<\/p><p>[&#8230;]<\/p><p>As the name implies this new API makes use of the <a href=\"https:\/\/en.wikipedia.org\/wiki\/Visitor_pattern\">visitor pattern<\/a>. Instead of implementing the hashing logic on the type <code>T<\/code> itself we are moving it into dedicated <code>Hasher<\/code> types, which then get passed to the objects to be hashed.<\/p><\/blockquote>\n\n<p>Here&rsquo;s <a href=\"https:\/\/gist.github.com\/regexident\/1b8e84974da2243e5199e760508d2d25\">his propoasal<\/a>.<\/p>\n\n<p>See also: <a href=\"https:\/\/github.com\/apple\/swift-evolution\/blob\/master\/proposals\/0185-synthesize-equatable-hashable.md\">SE-0185: Synthesizing <code>Equatable<\/code> and <code>Hashable<\/code> conformance<\/a>.<\/p>","protected":false},"excerpt":{"rendered":"<p>Vincent Esche (via Tyler Fox): In short: Hashable is utterly error-prone and does not compose well. And that&rsquo;s a shame![&#8230;]If even the official Swift documentation gets the implementation of hashValue wrong, then who is to expect the average Swift programmer to do any better?[&#8230;]Now, I&rsquo;m the first to admit that a scenario where one needs [&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":[46,71,901],"class_list":["post-19192","post","type-post","status-publish","format-standard","hentry","category-programming-category","tag-languagedesign","tag-programming","tag-swift-programming-language"],"apple_news_notices":[],"_links":{"self":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/19192","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=19192"}],"version-history":[{"count":1,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/19192\/revisions"}],"predecessor-version":[{"id":19193,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/19192\/revisions\/19193"}],"wp:attachment":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/media?parent=19192"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/categories?post=19192"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/tags?post=19192"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}