{"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=\"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><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=\"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>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_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],"tags":[292,13,185,173],"class_list":["post-1300","post","type-post","status-publish","format-standard","hentry","category-delphi","tag-delphi","tag-language","tag-namespaces","tag-pascal"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/p1TKYv-kY","jetpack_sharing_enabled":true,"jetpack-related-posts":[],"_links":{"self":[{"href":"https:\/\/www.deltics.co.nz\/blog\/wp-json\/wp\/v2\/posts\/1300","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=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}]}}