{"id":806,"date":"2020-07-23T17:07:00","date_gmt":"2020-07-23T23:07:00","guid":{"rendered":"https:\/\/www.great-white-software.com\/blog\/?p=806"},"modified":"2020-07-22T17:34:32","modified_gmt":"2020-07-22T23:34:32","slug":"virtual-dispatch-for-computed-properties","status":"publish","type":"post","link":"https:\/\/www.great-white-software.com\/blog\/2020\/07\/23\/virtual-dispatch-for-computed-properties\/","title":{"rendered":"Virtual dispatch for computed properties"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">So what the heck is Norm on about NOW ?<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Properties. Specifically computed properties.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Normally you <a href=\"https:\/\/www.great-white-software.com\/blog\/2019\/05\/28\/why-shadowing-should-be-avoided\/\">dont want to be shadowing properties<\/a> and if you do you need to be <a href=\"https:\/\/www.great-white-software.com\/blog\/2019\/06\/17\/follow-up-on-why-shadowing-should-be-avoided\/\">careful about how you do it<\/a> so it doesn&#8217;t muck up everything. But even when you DO shadow &#8220;right&#8221; there can still be issues.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">And the reason this comes up is because the DECLARED type of an item may be REALLY relevant. <\/p>\n\n\n\n<p class=\"wp-block-paragraph\">For instance if you have<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Class Item\n     property Foo as Integer\nEnd Class\n\nClass subItem\n  Inherits Item\n   \n    Computed Property Foo as integer\n      Get as Integer\n        break\n        return Item(self).Foo\n      End Get\n      Set(value as integer)\n        break\n        Item(self).Foo = value\n      End Set\n    End Computed Property\nEnd Class<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">This can lead to a messy situation. <\/p>\n\n\n\n<p class=\"wp-block-paragraph\">If you try something like<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Dim c As Item\n\nc = New Item\nc.foo = 129 \/\/ sets Foo directly and never hits a breakpoint in the computed\n\nc = New Subitem\nc.foo = 9 <\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">You will not get ANY complaints from the compiler<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The local we declared, c, can hold a reference to an Item OR any of its subclasses &#8211; since any of its subclasses IS also the super type (thats how inheritance works)<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">But with properties the DECLARED type matters. Although the debugger knows that, at the end c holds a reference to one of the SubTypes, the PROPERTIES that are accessed are those of the SUPER class in this case (actually what would be correct to say is the properties of the declared type)<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">And so, because despite a computed property being a lot like a pair of methods, its not treated the same. For instance, if the code above is altered to<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Class Item\n     Public Sub Foo() as Integer\n       break\n       return privateFoo\n     End Sub\n     Public Sub Foo(assigns v as Integer)\n       Break\n       privateFoo = v\n     End Sub\n\n     Private property privateFoo as integer\nEnd Class\n\nClass subItem\n  Inherits Item\n   \n     Public Sub Foo() as Integer\n       break\n       return super.Foo\n     End Sub\n     Public Sub Foo(assigns v as Integer)\n       Break\n       super.Foo = v\n     End Sub\nEnd Class<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">with the same code trying to set and get the value <\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Dim c As Item\n\nc = New Item\nc.foo = 129 \/\/ sets Foo directly and never hits a breakpoint in the computed\n\nc = New SubItem\nc.foo = 9 <\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">You&#8217;ll see a vast different. This time the RUNTIME type matters and the code in the subclass methods DOES get called.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">This is because methods, unlike properties, do use dynamic or virtual dispatch (the names get used interchangeably quite frequently) But properties do NOT. so whatever declared type a local or parameter is matters as to which property you might actually be accessing.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Sometimes this is where you need to cast (which kind of breaks the whole idea of using OO anyways since now you end up with big select case statements to handle many supers &amp; subclasses)<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">IF subclasses computed properties could be handled using the same dynamic dispatch mechanism that methods use it would make it possible to put properties in the inspector behaviour AND have subclass properties overload the super class ones.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Right now you have to pick between one or the other since computed properties can be in the Inspector Behaviour but arent virtual, or you use method pairs which ARE virtual but cant be exposed in the Inspector Behaviour.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Its crummy to have to make this choice at all.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>So what the heck is Norm on about NOW ? Properties. Specifically computed properties. Normally you dont want to be shadowing properties and if you do you need to be careful about how you do it so it doesn&#8217;t muck up everything. But even when you DO shadow &#8220;right&#8221; there can still be issues. And &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/www.great-white-software.com\/blog\/2020\/07\/23\/virtual-dispatch-for-computed-properties\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Virtual dispatch for computed properties&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_feature_clip_id":0,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_post_was_ever_published":false},"categories":[1],"tags":[],"class_list":["post-806","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/www.great-white-software.com\/blog\/wp-json\/wp\/v2\/posts\/806","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.great-white-software.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.great-white-software.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.great-white-software.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.great-white-software.com\/blog\/wp-json\/wp\/v2\/comments?post=806"}],"version-history":[{"count":1,"href":"https:\/\/www.great-white-software.com\/blog\/wp-json\/wp\/v2\/posts\/806\/revisions"}],"predecessor-version":[{"id":807,"href":"https:\/\/www.great-white-software.com\/blog\/wp-json\/wp\/v2\/posts\/806\/revisions\/807"}],"wp:attachment":[{"href":"https:\/\/www.great-white-software.com\/blog\/wp-json\/wp\/v2\/media?parent=806"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.great-white-software.com\/blog\/wp-json\/wp\/v2\/categories?post=806"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.great-white-software.com\/blog\/wp-json\/wp\/v2\/tags?post=806"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}