{"id":3019,"date":"2019-10-29T14:06:10","date_gmt":"2019-10-29T02:06:10","guid":{"rendered":"https:\/\/www.deltics.co.nz\/blog\/?p=3019"},"modified":"2019-10-29T14:06:20","modified_gmt":"2019-10-29T02:06:20","slug":"whats-in-a-namespace-2","status":"publish","type":"post","link":"https:\/\/www.deltics.co.nz\/blog\/posts\/3019\/","title":{"rendered":"What&#8217;s in a Name[space] ?"},"content":{"rendered":"<span class=\"span-reading-time rt-reading-time\" style=\"display: block;\"><span class=\"rt-label rt-prefix\">[Estimated Reading Time: <\/span> <span class=\"rt-time\"> 4<\/span> <span class=\"rt-label rt-postfix\">minutes]<\/span><\/span>\n<p class=\"wp-block-paragraph\">Namespaces in most modern languages are, in simple terms, a way to organise identifiers to avoid confusion (between those identifiers).  Typically a file will declare (or identify) the namespace for any identifiers it introduces, independently of the filename itself.   Conversely, identifiers in a namespace can be brought into scope where needed by referencing the namespace without needing to know which files contain the declarations for those identifiers.  Unfortunately the <strong>unit<\/strong> module in Pascal\/Delphi predates this by some margin and things are not quite so convenient.  But there are things we can do to improve matters.<\/p>\n\n\n\n<!--more-->\n\n\n\n<p class=\"wp-block-paragraph\">Namespace prefixes are one obvious tool that can be deployed in this area.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">These don&#8217;t quite provide the level of decoupling of files and namespaces that are found in other languages, and it could be argued that they are really just a kludge.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">They allow us to have files which contain alternate forms of otherwise identically named identifiers and to effectively &#8220;switch&#8221; between which set of files we wish to use.  Namespace prefixes are really little more than a slightly more complicated version of <strong>unit aliases<\/strong> so let&#8217;s do a quick recap of that.<\/p>\n\n\n\n<p class=\"has-drop-cap wp-block-paragraph\">Unit aliases can be useful when we decide to rename a unit and don&#8217;t wish to have to go through all of our existing code and change the references to the old name to the new one.  We can instead use an alias to let the compiler know that when it sees a reference to the old name treat it as a reference to the new on.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">As well as conveniently replacing\/renaming units however, we can also use a unit alias to select at compile-time one of any number of different files containing different, equally current implementations of a class (or other declarations, functions, variables, etc), each with the same name and the same public interface (i.e the same <code class=\"\" data-line=\"\">public<\/code> and <code class=\"\" data-line=\"\">published<\/code> methods\/properties).  Code that consumes these classes <em>could<\/em> compile with any of those possible units, but we <em>must<\/em> choose just one. <\/p>\n\n\n\n<p class=\"wp-block-paragraph\">For a simple, contrived example, let&#8217;s imagine we have a <code class=\"\" data-line=\"\">TWidgetService<\/code> class with two different implementations, <em>Flavours<\/em> <strong>A<\/strong> and <strong>B<\/strong>.  With a unit alias we would implement different <code class=\"\" data-line=\"\">TWidgetService<\/code> classes in each of two files:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>WidgetServiceFlavourA.pas<\/li><li>WidgetServiceFlavourB.pas<\/li><\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">And in a project consuming this service we would declare a unit alias that equates the <em>virtual<\/em> unit <strong>WidgetService<\/strong> to one or other of these, e.g.:<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><code class=\"\" data-line=\"\">WidgetService=&lt;strong&gt;WidgetServiceFlavour&lt;em&gt;A&lt;\/em&gt;&lt;\/strong&gt;<\/code><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Then in units (or libraries or programs) which <em>use<\/em> the service we would add the <em>virtual<\/em> unit to the <code class=\"\" data-line=\"\">uses<\/code> clause:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">program WidgetConsumerProgram;\n\nuses\n  <strong>WidgetService<\/strong>;\n\nvar\n  widget: TWidgetService;\n\n..<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Since the unit alias is a project level compiler setting, this enables us to &#8216;choose&#8217; which implementation of our service our project will be built against.  In this case the <code class=\"\" data-line=\"\">widget<\/code> variable will be a reference to an instance of the <code class=\"\" data-line=\"\">TWidgetService<\/code> class provided by the <code class=\"\" data-line=\"\">WidgetService&lt;strong&gt;FlavourA&lt;\/strong&gt;<\/code> unit.  To rebuild our project instead with <code class=\"\" data-line=\"\">WidgetService&lt;strong&gt;FlavourB&lt;\/strong&gt;<\/code> we simply change the unit alias:<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><code class=\"\" data-line=\"\">WidgetService=<\/code><strong><code class=\"\" data-line=\"\">WidgetServiceFlavour&lt;em&gt;B&lt;\/em&gt;<\/code><\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">And at a stroke the entire project is now built with the different flavour (B) of our <code class=\"\" data-line=\"\">TWidgetService<\/code>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">This is fine for <em>individual<\/em> units, but what if we have a whole host of classes and other identifiers in a framework, spread across multiple units ?  Changing unit aliases for all of the units involved is going to be tedious, not to mention error-prone.  This is where namespace prefixes come in and effectively provide &#8220;bulk unit aliasing&#8221;.<\/p>\n\n\n\n<p class=\"has-drop-cap wp-block-paragraph\">Namespace prefixes work by first assuming that you distinguish between your different flavours of identifiers in a conventional way, by which I mean: following a particular convention.  The convention in question being the use of a consistent unit name prefix for each flavour, separated from the &#8220;real&#8221; unit name by a dot.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">i.e. instead of <code class=\"\" data-line=\"\">WidgetServiceFlavourA<\/code> and <code class=\"\" data-line=\"\">WidgetServiceFlavourB<\/code>, rather:<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><code class=\"\" data-line=\"\">&lt;strong&gt;FlavourA&lt;\/strong&gt;.WidgetService<\/code> and <code class=\"\" data-line=\"\">&lt;strong&gt;FlavourB&lt;\/strong&gt;.WidgetService<\/code>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">With the introduction of namespace prefixes, the rules for unit resolution were extended to first look for a unit name that matches the un-prefixed name in a <code class=\"\" data-line=\"\">uses<\/code> clause with any filename that corresponds to that name plus a namespace prefix specified in the project compilation options.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">So just as with a unit alias, in your consuming code you reference the virtual unit name: <code class=\"\" data-line=\"\">WidgetService<\/code>.  The difference here of course is that you may have many, many units for each &#8216;flavour&#8217;.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><em>The resolved unit name and the physical filename must still match<\/em>.  The namespace prefix allows us to omit the first part of the unit filename when we reference it in a <code class=\"\" data-line=\"\">uses<\/code> clause, as long as a suitable prefix is supplied in the compiler settings to resolve the <em>partial<\/em> unit name to a full, physical unit name somewhere on the search path.  Then if there is a &#8216;mirror&#8217; universe of other units that exist with some other prefix we can switch to those by simply changing the prefix in our compiler settings.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Just like unit aliases.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The most obvious example of this is of course the various units that comprise the <strong>Vcl<\/strong> and <strong>Fmx<\/strong> (FireMonkey) frameworks.  Unit aliases could have been used to manage cross-framework compatibility but it doesn&#8217;t take much imagination to realise how painful this would have been with the large number of unit involved in those frameworks.<\/p>\n\n\n\n<p class=\"has-background has-pale-pink-background-color wp-block-paragraph\">If you look for documentation on namespaces in Delphi you will likely come across information that suggests far more is going on than this pretty blunt unit-aliasing tool, particularly this <a href=\"http:\/\/docs.embarcadero.com\/products\/rad_studio\/delphiAndcpp2009\/HelpUpdate2\/EN\/html\/devcommon\/usingnamespaces_xml.html\">section of the on-line help<\/a>.  As far as I can tell, this is simply hopelessly out-of-date documentation for the ill-fated Delphi.NET compiler.  The biggest clue to this is a reference to <code class=\"\" data-line=\"\">dcuil<\/code> in the section on <em>Declaring Namespaces<\/em>.  This is odd as the copyright date on the page indicates it being written in 2009, when Delphi.NET had been axed and replaced by <a href=\"https:\/\/www.elementscompiler.com\/elements\/oxygene\/\">Oxygene<\/a> (branded as Delphi Prism).<\/p>\n\n\n\n<p class=\"has-background has-light-green-cyan-background-color wp-block-paragraph\"><a href=\"https:\/\/www.elementscompiler.com\/elements\/oxygene\/\">Oxygene<\/a> has a completely reworked and explicit namespace syntax and wholly different (and far more straightforward!) namespace rules than those described.<\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<p class=\"wp-block-paragraph\">All of this is very interesting but is really just background for what I <em>really<\/em> want to talk about which is a pattern I have established for simplifying the consumption of multi-unit frameworks and libraries, such as <a href=\"https:\/\/github.com\/deltics\/deltics.smoketest\">Smoketest<\/a>.  I call this pattern (rightly or wrongly) <strong><em>Scope Elevation<\/em><\/strong> and I&#8217;ll explain exactly what it entails next time, using some of the new features in the upcoming <em>Smoketest 2.1.x<\/em> to illustrate how I find it useful.<\/p>\n","protected":false},"excerpt":{"rendered":"<p><span class=\"span-reading-time rt-reading-time\" style=\"display: block;\"><span class=\"rt-label rt-prefix\">[Estimated Reading Time: <\/span> <span class=\"rt-time\"> 4<\/span> <span class=\"rt-label rt-postfix\">minutes]<\/span><\/span>A recap of unit aliases and how they relate to namespace prefixes as a prelude to a more interesting examination of Scope Elevation.<\/p>\n","protected":false},"author":2,"featured_media":3022,"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_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2},"jetpack_post_was_ever_published":false},"categories":[4,1],"tags":[13,185,351],"class_list":["post-3019","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-delphi","category-general","tag-language","tag-namespaces","tag-unit-aliases"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"https:\/\/i0.wp.com\/www.deltics.co.nz\/blog\/wp-content\/uploads\/niklaus-wirth-edit.jpg?fit=934%2C362&ssl=1","jetpack_shortlink":"https:\/\/wp.me\/p1TKYv-MH","jetpack_sharing_enabled":true,"jetpack-related-posts":[],"_links":{"self":[{"href":"https:\/\/www.deltics.co.nz\/blog\/wp-json\/wp\/v2\/posts\/3019","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.deltics.co.nz\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.deltics.co.nz\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.deltics.co.nz\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.deltics.co.nz\/blog\/wp-json\/wp\/v2\/comments?post=3019"}],"version-history":[{"count":3,"href":"https:\/\/www.deltics.co.nz\/blog\/wp-json\/wp\/v2\/posts\/3019\/revisions"}],"predecessor-version":[{"id":3023,"href":"https:\/\/www.deltics.co.nz\/blog\/wp-json\/wp\/v2\/posts\/3019\/revisions\/3023"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.deltics.co.nz\/blog\/wp-json\/wp\/v2\/media\/3022"}],"wp:attachment":[{"href":"https:\/\/www.deltics.co.nz\/blog\/wp-json\/wp\/v2\/media?parent=3019"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.deltics.co.nz\/blog\/wp-json\/wp\/v2\/categories?post=3019"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.deltics.co.nz\/blog\/wp-json\/wp\/v2\/tags?post=3019"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}