{"id":243,"date":"2016-05-16T17:44:59","date_gmt":"2016-05-16T17:44:59","guid":{"rendered":"http:\/\/brian.cordanyoung.com\/Portfolio\/?p=243"},"modified":"2016-05-17T16:17:08","modified_gmt":"2016-05-17T16:17:08","slug":"protocol-programming-problem","status":"publish","type":"post","link":"https:\/\/brian.cordanyoung.com\/Portfolio\/protocol-programming-problem\/","title":{"rendered":"Protocol Programming Problem"},"content":{"rendered":"<p>I love the idea of protocol oriented programming. \u00a0So far, I&#8217;ve used it in a limited way. \u00a0But, while branching out I ran in to this case that has so far forced me to use more boiler plate code than I think I should (guard statements and casting of properties)<\/p>\n<p>I&#8217;m looking to understand why the type system doesn&#8217;t simply recognize that a property type conforms to a protocol, and this should allow the class to conform to a protocol. \u00a0I know. \u00a0That made no sense.<\/p>\n<p>But, this will. \u00a0Here are 2 protocols.<\/p>\n<pre class=\"lang:swift decode:true\" title=\"The Protocols\">\/\/\r\n\/\/ This Protocol defines a property that is required\r\nprotocol ProtocolReqiuringSomePropertyType\r\n{\r\n  var somePropertyRequiredByTheProtocol: Int {get}\r\n}\r\n\r\n\/\/ This Protocol defines a property that must conform to the\r\n\/\/ protocol defined above\r\nprotocol ProtocolUsingPropertyOfOtherProtocolType\r\n{\r\n    var someProperty: ProtocolReqiuringSomePropertyType {get set}\r\n}\r\n\r\n\/\/ This extention on the 2nd protocol accesses the\r\n\/\/ property from the first protocol\r\nextension ProtocolUsingPropertyOfOtherProtocolType\r\n{\r\n  func processSomePropertyOfThePorotcol(multiplier: Int) -&gt; Int {\r\n        return someProperty.somePropertyRequiredByTheProtocol * multiplier\r\n    }\r\n}\r\n\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p>Take a look at how I use the protocols in the objects below.<\/p>\n<pre class=\"lang:swift mark:20,23,33,36,37 decode:true\" title=\"The Objects\">\/\/\r\n\/\/ This struct conforms to the 1st protocol.\r\n\/\/ It has the required property and defines it's own property.\r\nstruct SomeStruct: ProtocolReqiuringSomePropertyType\r\n{\r\n    var somePropertyRequiredByTheProtocol = 0\r\n    var somePropertyOnTheStruct: Int = 10\r\n}\r\n\r\n\/\/ This class conforms to the 2nd protocol define above.\r\nclass SomeClass: ProtocolUsingPropertyOfOtherProtocolType\r\n{\r\n    \/\/ I want to define this property as type \"SomeStruct\".\r\n    \/\/ 'SomeStruct' conforms to 'ProtocolReqiuringSomePropertyType'\r\n    \/\/ I expected the type system to recognize this and allow it to be\r\n    \/\/ 'SomeStruct'.  But, it requires the property to be\r\n    \/\/ 'ProtocolReqiuringSomePropertyType'\r\n     \r\n    \/\/ Does NOT compile.\r\n    \/\/ var someProperty: SomeStruct = SomeStruct()\r\n    \r\n    \/\/ Does compile.\r\n    var someProperty: ProtocolReqiuringSomePropertyType = SomeStruct()\r\n    \r\n    func funcThatShowsTheProblem() -&gt; Int {\r\n\r\n        \r\n\t\/\/ Because I have to define 'someProperty' as type\r\n\t\/\/ 'ProtocolReqiuringSomePropertyType', I now have to cast the property to\r\n\t\/\/ 'SomeStruct' to access the property defined in the struct.\r\n         \r\n        \/\/ Does NOT compile.\r\n        \/\/ return self.someProperty.somePropertyOnTheStruct\r\n        \r\n        \/\/ Does compile.\r\n        guard let castProperty = someProperty as? SomeStruct else { return 0 }\r\n        return castProperty.somePropertyOnTheStruct\r\n    }\r\n}\r\n\r\n<\/pre>\n<p>In\u00a0<span style=\"color: #333399;\">SomeClass<\/span>, \u00a0I want to declare the property\u00a0<span style=\"color: #333399;\">someProperty<\/span>\u00a0as type\u00a0<span style=\"color: #333399;\">SomeStruct<\/span>. \u00a0<span style=\"color: #333399;\">SomeStruct<\/span>\u00a0conforms to\u00a0<span style=\"color: #333399;\">ProtocolReqiuringSomePropertyType<\/span>. \u00a0I would expect the type system to recognize this. \u00a0That should satisfy the class conformance to\u00a0<span style=\"color: #333399;\">ProtocolUsingPropertyOfOtherProtocolType<\/span>.<\/p>\n<p>But, the type system is forcing me to declare\u00a0<span style=\"color: #333399;\">someProperty<\/span>\u00a0as\u00a0<span style=\"color: #333399;\">ProtocolReqiuringSomePropertyType<\/span>. And that means I need to case the property to a type\u00a0<span style=\"color: #333399;\">SomeStruct<\/span>\u00a0each time I want to access the property defined in the struct.<\/p>\n<p>Am I missing a way to do this that doesn&#8217;t require casting <span style=\"color: #333399;\">SomeStruct<\/span> each time?<\/p>\n<h3>Update<\/h3>\n<p>Thank you to Ash Furrow (\u200f<a href=\"https:\/\/twitter.com\/ashfurrow\">@ashfurrow<\/a>) for the work around. It&#8217;s not my ideal for how swift would handle it, but it gets the job done.<\/p>\n<blockquote class=\"twitter-tweet\" data-lang=\"en\">\n<p dir=\"ltr\" lang=\"en\"><a href=\"https:\/\/twitter.com\/BrianCYoung\">@BrianCYoung<\/a> <a href=\"https:\/\/twitter.com\/NatashaTheRobot\">@NatashaTheRobot<\/a> this _kind of_ illustrates what I mean: <a href=\"https:\/\/t.co\/YoCZxythE9\">pic.twitter.com\/YoCZxythE9<\/a><\/p>\n<p>\u2014 Ash Furrow (@ashfurrow) <a href=\"https:\/\/twitter.com\/ashfurrow\/status\/732347424635785216\">May 16, 2016<\/a><\/p><\/blockquote>\n<p><script src=\"\/\/platform.twitter.com\/widgets.js\" async=\"\" charset=\"utf-8\"><\/script><\/p>\n<p><em>note: I still had to cast the computed property to the backing property to compile. But, at least this is only in a single location and not everytime I want to refer to <span style=\"color: #333399;\">someProperty<\/span>.<\/em><\/p>\n<pre class=\"lang:swift decode:true \">\/\/\r\nclass SomeClass: ProtocolUsingPropertyOfOtherProtocolType\r\n{\r\n    var _someProperty = SomeStruct()\r\n    var someProperty: ProtocolReqiuringSomePropertyType {\r\n        get {\r\n            return _someProperty\r\n        }\r\n        set(newValue) {\r\n            guard let castValue = newValue as? SomeStruct\r\n                else {\r\n                    assertionFailure(\"someProperty  must be of type SomeStruct\")\r\n                    return}\r\n            _someProperty = castValue\r\n        }\r\n    }\r\n\r\n    func FuncThatShowsTheProblem() -&gt; Int {\r\n         return self._someProperty.somePropertyOnTheStruct\r\n    }\r\n}\r\n\r\n\r\n<\/pre>\n<p><a href=\"wp-content\/uploads\/2016\/05\/ProtocolTypes-v2.playground.zip\">Download Playground<\/a><\/p>\n<p><a name=\"update2\"><\/a><\/p>\n<h3>Update Too<\/h3>\n<p>There is nothing like a good sleep and a long drive to let the mind wander. In this case, my mind wandered to another weak spot of my swift knowledge. \u00a0Generics, AssociatedTypes and the where clause. \u00a0This compiles and looks much better.<\/p>\n<pre class=\"lang:swift mark:12,13,18,36 decode:true\">\/\/\r\n\/\/ This Protocol defines a property that is required\r\nprotocol ProtocolReqiuringSomePropertyType\r\n{\r\n  var somePropertyRequiredByTheProtocol: Int {get}\r\n}\r\n\r\n\/\/ This Protocol defines a property that must conform to the\r\n\/\/ protocol defined above\r\nprotocol ProtocolUsingPropertyOfOtherProtocolType\r\n{\r\n    associatedtype T\r\n    var someProperty: T {get set}\r\n}\r\n\r\n\/\/ This extention on the 2nd protocol accesses the\r\n\/\/ property from the first protocol\r\nextension ProtocolUsingPropertyOfOtherProtocolType where T: ProtocolReqiuringSomePropertyType\r\n{\r\n    func processSomePropertyOfThePorotcol(multiplier: Int) -&gt; Int {\r\n        return someProperty.somePropertyRequiredByTheProtocol * multiplier\r\n    }\r\n}\r\n\r\n\/\/ This struct conforms to the 1st protocol.\r\n\/\/ It has the required property and defines it's own property.\r\nstruct SomeStruct: ProtocolReqiuringSomePropertyType\r\n{\r\n    var somePropertyRequiredByTheProtocol = 0\r\n    var somePropertyOnTheStruct: Int = 10\r\n}\r\n\r\n\/\/ This class conforms to the 2nd protocol define above.\r\nclass SomeClass: ProtocolUsingPropertyOfOtherProtocolType\r\n{\r\n    var someProperty: SomeStruct = SomeStruct()\r\n    \r\n    func FuncThatShowsTheProblem() -&gt; Int {\r\n         return self.someProperty.somePropertyOnTheStruct\r\n    }\r\n}\r\n<\/pre>\n<p><a href=\"wp-content\/uploads\/2016\/05\/ProtocolTypes-v3.playground.zip\">Download Playground<\/a><\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I love the idea of protocol oriented programming. \u00a0So far, I&#8217;ve used it in a limited way. \u00a0But, while branching out I ran in to this case that has so far forced me to use more boiler plate code than I think I should (guard statements and casting of properties) I&#8217;m looking to understand why [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[12],"tags":[],"class_list":["post-243","post","type-post","status-publish","format-standard","hentry","category-blog"],"_links":{"self":[{"href":"https:\/\/brian.cordanyoung.com\/Portfolio\/wp-json\/wp\/v2\/posts\/243","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/brian.cordanyoung.com\/Portfolio\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/brian.cordanyoung.com\/Portfolio\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/brian.cordanyoung.com\/Portfolio\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/brian.cordanyoung.com\/Portfolio\/wp-json\/wp\/v2\/comments?post=243"}],"version-history":[{"count":36,"href":"https:\/\/brian.cordanyoung.com\/Portfolio\/wp-json\/wp\/v2\/posts\/243\/revisions"}],"predecessor-version":[{"id":351,"href":"https:\/\/brian.cordanyoung.com\/Portfolio\/wp-json\/wp\/v2\/posts\/243\/revisions\/351"}],"wp:attachment":[{"href":"https:\/\/brian.cordanyoung.com\/Portfolio\/wp-json\/wp\/v2\/media?parent=243"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/brian.cordanyoung.com\/Portfolio\/wp-json\/wp\/v2\/categories?post=243"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/brian.cordanyoung.com\/Portfolio\/wp-json\/wp\/v2\/tags?post=243"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}