{"id":1300,"date":"2012-11-28T09:10:40","date_gmt":"2012-11-27T21:10:40","guid":{"rendered":"https:\/\/www.deltics.co.nz\/blog\/?p=1300"},"modified":"2012-11-28T11:28:07","modified_gmt":"2012-11-27T23:28:07","slug":"whats-in-a-namespace","status":"publish","type":"post","link":"https:\/\/www.deltics.co.nz\/blog\/posts\/1300\/","title":{"rendered":"What&#8217;s in a Name(space) ?"},"content":{"rendered":"<span class=\"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><p>The ever evolving DWScript project continues to advance the Pascal language at an impressive pace.  Just today it was announced that this scripting version of Pascal <a href=\"http:\/\/delphitools.info\/2012\/11\/27\/introducing-unit-namespace\/\" target=\"_blank\">now has &#8220;namespace&#8221; support<\/a>.<br \/>\n<!--more--><br \/>\nWhen I first read the details of the implementation, my initial reaction was that it &#8220;felt a bit backwards&#8221;.  The essence of the implementation is that it involves a new syntax for a unit, which instead of representing an actual unit, creates a &#8220;virtual&#8221; unit that effectively acts to collate or aggregate a number of other units into a single name:<\/p>\n<pre class=\"brush: delphi; title: ; notranslate\" title=\"\">\r\n   unit namespace Foo.Bar;\r\n\r\n   uses\r\n     Foo.Bar.One,\r\n     Foo.Bar.Two;\r\n<\/pre>\n<p>Then in some other unit you can use both Foo.Bar.One and Foo.Bar.Two by simply using the namespace that is thus declared as using it:<\/p>\n<pre class=\"brush: delphi; title: ; notranslate\" title=\"\">\r\n  unit MyUnit;\r\n\r\n  uses\r\n    Foo.Bar;   \/\/ uses Foo.Bar.One and Foo.Bar.Two\r\n<\/pre>\n<p>As I say, whilst useful and powerful, it feels a bit backwards to me.<\/p>\n<p>That is, you have to declare a namespace and then say what is in it, when it seems more natural to me for a unit to say which namespace it is a part of, itself.  Apart from anything else, it seems to me that using this &#8220;<em>this namespace consists of<\/em>&#8221; approach (vs &#8220;<em>I am part of a namespace<\/em>&#8220;) you can end up with two units in two different namespaces, no ?<\/p>\n<p>e.g. given:<\/p>\n<pre class=\"brush: delphi; title: ; notranslate\" title=\"\">\r\n   unit namespace Foo.Bar;\r\n\r\n   uses\r\n     Foo.Bar.One,\r\n     Foo.Bar.Two;\r\n<\/pre>\n<p>and<\/p>\n<pre class=\"brush: delphi; title: ; notranslate\" title=\"\">\r\n   unit namespace Snafu;\r\n\r\n   uses\r\n     Snafu.One,\r\n     Foo.Bar.One;\r\n<\/pre>\n<p>Having said that, whilst this might make me a little uncomfortable, is it actually a problem as such ?  If a unit then uses both <strong>Foo.Bar<\/strong> and <strong>Snafu<\/strong> namespaces, presumably the aggregated unit lists that comprise those namespaces will be reduced to a list of unique units.<\/p>\n<p>So perhaps not a problem at all.<\/p>\n<p>In fact, it could be seen as a feature, since it would &#8211; presumably &#8211; allow you to have a namespace that brings an <em>entire other namespace<\/em> with it.  i.e. <strong>Snafu<\/strong> might itself bring in the entire <strong>Foo.Bar<\/strong> namespace, so that if your unit then uses <strong>Snafu<\/strong>, it is automatically using not just <strong>Snafu<\/strong> itself but also the entire <strong>Foo.Bar<\/strong> namespace, without having to declare this separately.<\/p>\n<p>Convenient, certainly, but what is an irritant when creating code (having to declare used units) can often be a boon when it comes to maintenance (being able to see <em>at a glance<\/em> what units are used by any given unit).<\/p>\n<h3>Caveat Scriptor<\/h3>\n<p>I should add at this point that this post is not intended to critique or suggest an alternative approach for <strong>DWScript<\/strong> itself.  Rather it is inspired by thinking about how such a feature would work in an actual, compiled Pascal project &#8211; specifically Delphi.<\/p>\n<h3>Looking Forward&#8230;<\/h3>\n<p>So, <strong>DWScript<\/strong> has it&#8217;s new namespace declaration unit, but I think even that can be made a bit more Delphi-like if it were to be adopted by Delphi itself.<\/p>\n<p>Delphi already has a module type which acts as &#8211; and thus has an appropriate syntax for &#8211; a container for other units.  The <strong>package<\/strong>.<\/p>\n<p>We need to change the module keyword, but that&#8217;s OK and indeed desirable, since we are now defining an entirely new module type, giving us a great deal more flexibility in devising an appropriate syntax for that module.  But we needn&#8217;t work <em>too<\/em> hard, since a lot of the existing package syntax &#8220;just fits&#8221;:<\/p>\n<pre class=\"brush: delphi; title: ; notranslate\" title=\"\">\r\n  namespace Foo.Bar;\r\n\r\n  contains\r\n    Foo.Bar.One in '..\\Foo\\Foo.Bar.One.Pas',\r\n    Foo.Bar.Two in '..\\Foo\\Foo.Bar.Two.Pas';\r\n  end.\r\n<\/pre>\n<p>This syntax allows us to better reflect the &#8220;containment&#8221; relationship that a namespace has with it&#8217;s members, as opposed to the &#8220;uses&#8221; declaration.  But this still has us declaring a namespace and then identifying it&#8217;s contents.  It still &#8220;feels backwards&#8221;.<\/p>\n<p>It makes more sense &#8211; to me anyway &#8211; for a unit to declare it&#8217;s namespace and, continuing the theme of recycling existing syntax where it fits, there is just such a syntax that works in this case.  I deliberately invoked that syntax in my &#8220;namespace&#8221; module example above:<\/p>\n<p>The <strong>in<\/strong> clause.<\/p>\n<p>We could simply [ \ud83d\ude42 ] add support for that to the module declaration in a unit:<\/p>\n<pre class=\"brush: delphi; title: ; notranslate\" title=\"\">\r\n   unit Foo.Bar.One in Foo.Bar;\r\n<\/pre>\n<p>This seems very neat and elegant to me but it has one major problem.  As Andreas Hausladen mentioned in the comments on the DWScript post:  the one-pass compiler in Delphi.<\/p>\n<p>When the compiler finds a unit that &#8220;<strong>uses Foo.Bar<\/strong>&#8221; it doesn&#8217;t know that <strong>Foo.Bar<\/strong> <em>is<\/em> a namespace or which units are contained <em>within<\/em> that namespace.  It won&#8217;t find out that <strong>Foo.Bar.One<\/strong> even exists or that it is part of <strong>Foo.Bar<\/strong> unless and until it is asked to reference <strong>Foo.Bar.One<\/strong> itself, which sort of defeats the purpose of namespaces, right out of the gate.<\/p>\n<p>If this could be solved as elegantly as the syntax for declaring the membership of a namespace itself (using this <strong>in<\/strong> syntax) then I think we&#8217;d have a winner.  \ud83d\ude42<\/p>\n<p>One way perhaps of resolving the, uh, resolving problem might be to introduce an initial first-pass in the compilation of a Delphi project, specifically to resolve namespaces.  This pass would use the existing <strong>uses<\/strong> declarations in the <strong>dpr<\/strong> combined with the project (and environment) search path (i.e. the current &#8220;unit search&#8221; algorithm) to assemble a directory of units and their corresponding namespace &#8216;containers&#8217;, where applicable.<\/p>\n<p>The bulk of this work &#8211; the business of locating a given unit on the search path &#8211; is already done by the compiler.  But with a directory of unit locations produced by the first pass, the compiler would be relieved of this effort.  Consequently the &#8220;cost&#8221; of the first pass would be offset &#8211; at least in part &#8211; by the removal of this effort from the compiler itself.<\/p>\n<p>It is also worth mentioning that even the &#8220;namespace module&#8221; would introduce some problems to be solved in any Delphi compiler implementation, since any namespace implementation creates the possibility, and increases the likelihood, of duplicate references to units in a single uses clause:<\/p>\n<pre class=\"brush: delphi; title: ; notranslate\" title=\"\">\r\n  uses\r\n    Foo.Bar,       \/\/ uses Foo.Bar.One and Foo.Bar.Two\r\n    Snafu;         \/\/ uses Snafu.One and Foo.Bar.One\r\n<\/pre>\n<p>Currently, if a unit is referenced more than once in a uses clause (or indeed is referenced in both interface and implementation uses clauses) then a compilation error results.  Either this would have to be relaxed to a warning or ignored entirely, if namespaces are to be implemented.<\/p>\n<p>This is perhaps less of a potential problem when a unit is confined to a single namespace, as would be the case if namespace membership were a declared aspect of a unit itself.  But still a potential problem.<\/p>\n<h3>The Undiscovered Country<\/h3>\n<p>Luckily, whilst we might speculate and invent a future namespace feature for Delphi, it doesn&#8217;t actually exist as yet (as far as I know).  This means we get to decide how we might like that future to actually look, from among the various options.<\/p>\n<p>So which would you prefer:<\/p>\n<ul>\n<li>A namespace module, declaring the units contained in the namespace<\/li>\n<li>An <strong>in<\/strong> declaration on a unit, with a name resolution pass added to the compiler<\/li>\n<li>Some other approach ?<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p><span class=\"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> The ever evolving DWScript project continues to advance the Pascal language at an impressive pace. Just today it was announced that this scripting version of Pascal now has &#8220;namespace&#8221; support.<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"jetpack_publicize_message":"","jetpack_is_tweetstorm":false,"jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":[]},"categories":[4],"tags":[292,13,185,173],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/p1TKYv-kY","jetpack_sharing_enabled":true,"jetpack-related-posts":[{"id":1817,"url":"https:\/\/www.deltics.co.nz\/blog\/posts\/1817\/","url_meta":{"origin":1300,"position":0},"title":"Getting the Battery Level on Android With Delphi","date":"01 Oct 2013","format":false,"excerpt":"Over the past few days I posted a two part series showing how to obtain the current battery level as part of the implementation of an Android AppWidget using Oxygene. As far as I can tell AppWidgets simply aren't possible using Delphi but reading the battery is quite straightforward Android\u2026","rel":"","context":"In &quot;Android&quot;","img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":3019,"url":"https:\/\/www.deltics.co.nz\/blog\/posts\/3019\/","url_meta":{"origin":1300,"position":1},"title":"What&#8217;s in a Name[space] ?","date":"29 Oct 2019","format":false,"excerpt":"A recap of unit aliases and how they relate to namespace prefixes as a prelude to a more interesting examination of Scope Elevation.","rel":"","context":"In &quot;Delphi&quot;","img":{"alt_text":"","src":"https:\/\/i0.wp.com\/www.deltics.co.nz\/blog\/wp-content\/uploads\/niklaus-wirth-edit.jpg?fit=934%2C362&ssl=1&resize=350%2C200","width":350,"height":200},"classes":[]},{"id":3024,"url":"https:\/\/www.deltics.co.nz\/blog\/posts\/3024\/","url_meta":{"origin":1300,"position":2},"title":"Scope Elevation: Creating A &#8216;Pseudo-Namespace&#8217;","date":"31 Oct 2019","format":false,"excerpt":"In my previous post I talked about how \"namespaces\" in Delphi really don't exist for any practical purposes normally associated with the concept. Having become familiar with the concept in other languages I found I was missing them, so I devised a way to obtain some of the benefits, despite\u2026","rel":"","context":"In &quot;Delphi&quot;","img":{"alt_text":"","src":"https:\/\/i0.wp.com\/www.deltics.co.nz\/blog\/wp-content\/uploads\/niklaus-wirth-edit.jpg?fit=934%2C362&ssl=1&resize=350%2C200","width":350,"height":200},"classes":[]},{"id":706,"url":"https:\/\/www.deltics.co.nz\/blog\/posts\/706\/","url_meta":{"origin":1300,"position":3},"title":"Making a case for Strings, the sane way","date":"02 Dec 2010","format":false,"excerpt":"Lars Fosdal responded to my previous post suggesting a way of implementing string support in a case-like construct (but not actually a case statement) using generics and anonymous methods. All very clever, but way, way too complicated and - if you don't mind me saying so - as ugly as\u2026","rel":"","context":"In &quot;Delphi&quot;","img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":1207,"url":"https:\/\/www.deltics.co.nz\/blog\/posts\/1207\/","url_meta":{"origin":1300,"position":4},"title":"Adventures in Syntax: Something Old, Something New etc&#8230;","date":"20 Sep 2012","format":false,"excerpt":"As the post title says, this will be a brief detour through some features of the Pascal language and a presentation of some (theoretical) alternatives that could have been introduced instead. That is, some are real but little known syntax, others are what I think might be preferable to the\u2026","rel":"","context":"In &quot;Delphi&quot;","img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":1945,"url":"https:\/\/www.deltics.co.nz\/blog\/posts\/1945\/","url_meta":{"origin":1300,"position":5},"title":"Unit Aliases and Build Configurations","date":"17 Oct 2013","format":false,"excerpt":"Over on stackoverflow, Ann Gossard had a question about using an $ifdef in the project (DPR) uses list to use one unit in preference over another, specifically in debug builds. In this situation, it immediately occurred to me that unit aliases might be creatively deployed. What follows is a slightly\u2026","rel":"","context":"In &quot;Delphi&quot;","img":{"alt_text":"","src":"https:\/\/i0.wp.com\/www.deltics.co.nz\/blog\/wp-content\/uploads\/Screen-Shot-2013-10-17-at-15.25.51.png?resize=350%2C200&ssl=1","width":350,"height":200},"classes":[]}],"_links":{"self":[{"href":"https:\/\/www.deltics.co.nz\/blog\/wp-json\/wp\/v2\/posts\/1300"}],"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=1300"}],"version-history":[{"count":9,"href":"https:\/\/www.deltics.co.nz\/blog\/wp-json\/wp\/v2\/posts\/1300\/revisions"}],"predecessor-version":[{"id":1309,"href":"https:\/\/www.deltics.co.nz\/blog\/wp-json\/wp\/v2\/posts\/1300\/revisions\/1309"}],"wp:attachment":[{"href":"https:\/\/www.deltics.co.nz\/blog\/wp-json\/wp\/v2\/media?parent=1300"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.deltics.co.nz\/blog\/wp-json\/wp\/v2\/categories?post=1300"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.deltics.co.nz\/blog\/wp-json\/wp\/v2\/tags?post=1300"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}