{"id":10664,"date":"2015-02-01T19:43:14","date_gmt":"2015-02-02T00:43:14","guid":{"rendered":"http:\/\/mjtsai.com\/blog\/?p=10664"},"modified":"2015-02-01T19:43:15","modified_gmt":"2015-02-02T00:43:15","slug":"phantom-types-and-typed-notification-observers","status":"publish","type":"post","link":"https:\/\/mjtsai.com\/blog\/2015\/02\/01\/phantom-types-and-typed-notification-observers\/","title":{"rendered":"Phantom Types and Typed Notification Observers"},"content":{"rendered":"<p><a href=\"http:\/\/www.objc.io\/snippets\/13.html\">objc.io<\/a>:<\/p>\n<blockquote cite=\"http:\/\/www.objc.io\/snippets\/13.html\">\n<p>Using these types, we can create two more wrappers, but this time around <code>NSFileHandle<\/code>&rsquo;s initializers. We&rsquo;ll use the <code>Read<\/code> and <code>Write<\/code> types, even though the <code>FileHandle<\/code> doesn&rsquo;t contain any values of that type. That&rsquo;s why the <code>A<\/code> parameter is called a <em>phantom type<\/em>. The types are just there to track permissions.<\/p>\n<p>[&#8230;]<\/p>\n<p>Trying to call this function on a file-handle that&rsquo;s open for writing will not even compile. You can use these techniques for important parts of your own code as well, if you have areas where you want the compiler to check errors for you, rather than having the code fail at runtime.<\/p>\n<\/blockquote>\n<p><a href=\"http:\/\/www.objc.io\/snippets\/16.html\">objc.io<\/a>:<\/p>\n<blockquote cite=\"http:\/\/www.objc.io\/snippets\/16.html\"><p>Finally, we can create our observer object. Creating an instance of this object adds an observer to <code>NSNotificationCenter<\/code>, and when it deallocs, it removes itself from the notification center. This allows us to store it as a property on an object, and once the property gets set to <code>nil<\/code> (e.g. when the object deallocs), it automatically removes itself from the notification center.<\/p><\/blockquote>\n<p>Then they use phantom types to make sure that a given notification is always posted with the proper type of user info object.<\/p>","protected":false},"excerpt":{"rendered":"<p>objc.io: Using these types, we can create two more wrappers, but this time around NSFileHandle&rsquo;s initializers. We&rsquo;ll use the Read and Write types, even though the FileHandle doesn&rsquo;t contain any values of that type. That&rsquo;s why the A parameter is called a phantom type. The types are just there to track permissions. [&#8230;] Trying to [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","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":[69,71,901],"class_list":["post-10664","post","type-post","status-publish","format-standard","hentry","category-programming-category","tag-cocoa","tag-programming","tag-swift-programming-language"],"apple_news_notices":[],"_links":{"self":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/10664","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=10664"}],"version-history":[{"count":1,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/10664\/revisions"}],"predecessor-version":[{"id":10665,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/10664\/revisions\/10665"}],"wp:attachment":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/media?parent=10664"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/categories?post=10664"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/tags?post=10664"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}