{"id":213,"date":"2019-08-08T15:35:08","date_gmt":"2019-08-08T21:35:08","guid":{"rendered":"https:\/\/www.great-white-software.com\/blog\/?p=213"},"modified":"2019-08-05T13:07:26","modified_gmt":"2019-08-05T19:07:26","slug":"leaky-abstractions-and-how-to-plug-some","status":"publish","type":"post","link":"https:\/\/www.great-white-software.com\/blog\/2019\/08\/08\/leaky-abstractions-and-how-to-plug-some\/","title":{"rendered":"Leaky abstractions and how to plug some"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">Every once in a while you run into some code that tries to present a fairly abstract API to some class \/ module. Maybe this is in your own code or in code you have received from some other source.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">But as you work with it you find that the abstraction reveals internal implementation details that really should not be known outside the class. One way to hide these sorts of details from other portions of your code is to use an API defined in an interface.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">However, even that doesnt always work.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Suppose you have some set of classes where you want to make it possible for one to contain many of the others and that the internal storage mechanism should be invisible, or at least not easily guessable, from other classes. Something like you see in the IDE where you have Folders that can contain all kinds of other items, or Modules that can also contains other items.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">It would make sense for the classes that represent such a Container type to implement the Xojo.Core.Iterable interface so you could use either in a For &#8230; Next loop.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Lets start by assuming we&#8217;re going to create a set up like <\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Class ContainerType\n   Implements Xojo.Core.Iterator\n\n   Function MoveNext() as Boolean\n   End Function\n\n   Function Value() as Auto\n   End Function\nEnd Class\n\nClass FolderType\n   Inherits ContainerType\nEnd Class\n\nClass ModuleType\n   Inherits ContainerType\nEnd Class<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">We&#8217;ll have a base ContainerType class and then two subclasses*. The names of the classes I&#8217;ve chosen don&#8217;t conflict with built in ones.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">At this point you might think you could write<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Dim f As New ContainerType\n\nFor Each foo As Auto In f\n    \nNext<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">but you can&#8217;t. In a for .. each you need an item that implements Xojo.Core.Iterator &#8211; not Xojo.Core.Iterable.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">OK we need an iterator so we&#8217;ll add one like<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Class ContainerTypeIterator\n  Implements Xojo.Core.Iterator\n\n   Function MoveNext() as Boolean\n   End Function\n\n   Function Value() as Auto\n   End Function\nEnd Class<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">As well we need to make our initial class implement Xojo.Core.Iterable<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Class ContainerType\n   Implements Xojo.Core.Iterable\n\n   Function GetIterator() as Xojo.Core.Iterator\n   End Function\n\nEnd Class\n\nClass FolderType\n   Inherits ContainerType\nEnd Class\n\nClass ModuleType\n   Inherits ContainerType\nEnd Class<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">And now we can write<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Dim f As New ContainerType \/\/ or new FolderType or new ModuleType\n\nFor Each foo As Auto In f\n    \nNext<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">But how do we make the Iterator itself work ? Unfortunately there don&#8217;t seem to be any Xojo code examples and the docs are kind of light on this.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The iterator isnt a &#8220;friend&#8221; of our base class so it cannot reach into its guts and grab whatever data it wants. Especially not if that data is protected or private in any way. So our Iterator needs some api to be able to get the data it needs from our ContainerType classes. So we need an API for that.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">But whatever API we put on the ContainerType class is also going to be usable outside the Iterator. There&#8217;s no way straight forward simple way to restrict an API to ONLY being by one class in Xojo. (This is where &#8220;friend&#8221; classes would be handy as they could have a &#8220;special&#8221; relationship and be allowed to use some private api that no other class could &#8211; there are hacky ways to achieve this though)<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">And so however you implement the Iterator &amp; its access to the individual members of each class that implements Iterable those API&#8217;s are usable by ANY other code. And so that abstraction of &#8220;iterable&#8221; and how it&#8217;s implemented leaks out to the rest of the world.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Stopping this leakage can be done &#8211; in a limited way. In a module you can have an interafce that is private to that module and then classes that are in that module can implement that interface. Only methods and other classes in that module can then use that interface.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Module Containers\n  Private Interface PrivateContainerAccessors\n     Function MoveNext() as boolean\n     Function Value() as Variant\n  End Interface\n\n  Class ContainerType\n    Implements Xojo.Core.Iterable\n\n    Function GetIterator() as Xojo.Core.Iterator\n    End Function\n\n     Function MoveNext() as boolean\n     End Function\n\n     Function Value() as Variant\n     End Function\n\n  End Class\n\n  Class FolderType\n    Inherits ContainerType\n\n     Function MoveNext() as boolean\n     End Function\n\n     Function Value() as Variant\n     End Function\n\n  End Class\n\n  Class ModuleType\n    Inherits ContainerType\n     Function MoveNext() as boolean\n     End Function\n\n     Function Value() as Variant\n     End Function\n\n  End Class\nEnd Module\n      <\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">And now only classes IN the module can cast the classes that implement this interface in a way they can call the interface methods. This is quite handy &#8211; but it does have a drawback. Nothing outside this module can implement that interface. And that limits its usefulness to code you write and put in that module.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Friend scope would be really handy. In the mean time this is as close as you get and can stop your Iterable abstraction and the implementation from leaking out into the rest of your code.<\/p>\n\n\n\n<p class=\"has-small-font-size wp-block-paragraph\">*This really is JUST an example and not actual Xojo IDE code for those of you who might be suspiciously minded &#8211; I can assure you the IDE is vastly different than this &#8211; this is JUST an example that you can relate to from using the IDE<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Every once in a while you run into some code that tries to present a fairly abstract API to some class \/ module. Maybe this is in your own code or in code you have received from some other source. But as you work with it you find that the abstraction reveals internal implementation details &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/www.great-white-software.com\/blog\/2019\/08\/08\/leaky-abstractions-and-how-to-plug-some\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Leaky abstractions and how to plug some&#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-213","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\/213","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=213"}],"version-history":[{"count":3,"href":"https:\/\/www.great-white-software.com\/blog\/wp-json\/wp\/v2\/posts\/213\/revisions"}],"predecessor-version":[{"id":225,"href":"https:\/\/www.great-white-software.com\/blog\/wp-json\/wp\/v2\/posts\/213\/revisions\/225"}],"wp:attachment":[{"href":"https:\/\/www.great-white-software.com\/blog\/wp-json\/wp\/v2\/media?parent=213"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.great-white-software.com\/blog\/wp-json\/wp\/v2\/categories?post=213"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.great-white-software.com\/blog\/wp-json\/wp\/v2\/tags?post=213"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}