www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - package.d behavior

--001a11369874d2fbca04f03dd98e
Content-Type: text/plain; charset=ISO-8859-1

Hello!

About a month ago I asked in D.learn about the expected behavior of the new
package.d feature [1]. I got a cuple of responses, but not really answers.
Today I noticed a second post [2] with similar but still unanswered
questions. So it seemed like a good idea to bring the discussion to the
main forum.

Basically, I seems that the root of the issues we are facing is the way
fully-qualified names work when using package.d (I have added some examples
below showing what I mean).

We also felt that the feature is under-documented (DIP37 and the changelog
seem to be the only places where the feature is discussed, and some
important details are missing.) I was actually about to fill bug a report
about the behavior, but I ended up not doing so because I couldn't find out
what the expected behavior is.

So, any feedback and clarifications are welcome!

Thanks for the attention, and keep up the great work :-)

LMB

[1]
http://forum.dlang.org/thread/CANY+vSMzLJ5ehKGW8cE1KkoMOm7x3roKmVgMjyCqZrwD9aLO9w mail.gmail.com
[2] http://forum.dlang.org/thread/eeaslvjwenkygwszqznc forum.dlang.org


-----------------------

EXAMPLE 1: Trying to simply replace the old "all.d" idiom with package.d
doesn't work out-of-the-box:

Originally, I had something like this:

 // mylib/util.d:
 module mylib.util;
 class Foo { }

 // mylib/all.d:
 module mylib.all;
 public import mylib.util;

 // main.d:
 import mylib.all;
 void main()
 {
    auto f = new mylib.util.Foo;
 }

And this used to work. Now, I added a new file:

 // package.d
 module mylib;
 public import mylib.util;

And changed the 'import' in the main one:

 // main.d
 import mylib;

 void main()
 {
    auto f = new mylib.util.Foo;
 }

Now, the compiler complains:

 main.d(5): Error: undefined identifier 'util'
 main.d(5): Error: mylib.util.Foo is used as a type

[Using mylib.Foo instead of mylib.util.Foo works -- which makes sense when
thnking about the use case of using package.d to split a large package into
smaller ones. ]


---------------------

EXAMPLE 2: Inconsistency with fully-qualified names

// mylib/util.d
module mylib.util;
class Foo { }

// mylib/package.d
module mylib;
public import mylib.util;

// main.d
import std.stdio;
import mylib;

void main()
{
   auto f = new mylib.Foo;
   writefln("%s", f.classinfo.name);
}

This prints 'mylib.util.Foo'. So far so good, that's the name I originally
expected.

Then I try to instantiate a 'Foo' using this very fully-qualified name the
compiler told me:

   auto f = new mylib.util.Foo;

And DMD doesn't like it anymore:

main.d(6): Error: undefined identifier 'util'
main.d(6): Error: mylib.util.Foo is used as a type

[This looks very much like a bug for me. The name given by
classinfo.nameshould be usable to instantiate a class, shouldn't it? ]

--001a11369874d2fbca04f03dd98e
Content-Type: text/html; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable

<div dir=3D"ltr"><div><div>Hello!<br><br></div>About a month ago I asked in=
 D.learn about the expected behavior of the new package.d feature [1]. I go=
t a cuple of responses, but not really answers. Today I noticed a second po=
st [2] with similar but still unanswered questions. So it seemed like a goo=
d idea to bring the discussion to the main forum.<br>

<br></div><div><div>Basically, I seems that the root of the issues we are f=
acing is the way fully-qualified names work when using package.d (I have ad=
ded some examples below showing what I mean).<br><br>We also felt that the =
feature is under-documented (DIP37 and the changelog seem to be the only pl=
aces where the feature is discussed, and some important details are missing=
.) I was actually about to fill bug a report about the behavior, but I ende=
d up not doing so because I couldn&#39;t find out what the expected behavio=
r is.<br>

<br></div><div>So, any feedback and clarifications are welcome!<br><br></di=
v><div>Thanks for the attention, and keep up the great work :-)<br></div><d=
iv><br></div><div></div><div>LMB<br><br>[1] <a href=3D"http://forum.dlang.o=
rg/thread/CANY+vSMzLJ5ehKGW8cE1KkoMOm7x3roKmVgMjyCqZrwD9aLO9w mail.gmail.co=
m">http://forum.dlang.org/thread/CANY+vSMzLJ5ehKGW8cE1KkoMOm7x3roKmVgMjyCqZ=
rwD9aLO9w mail.gmail.com</a><br>

[2] <a href=3D"http://forum.dlang.org/thread/eeaslvjwenkygwszqznc forum.dla=
ng.org">http://forum.dlang.org/thread/eeaslvjwenkygwszqznc forum.dlang.org<=
/a><br><br><br>-----------------------<br><br></div><div>EXAMPLE 1: Trying =
to simply replace the old &quot;all.d&quot; idiom with package.d doesn&#39;=
t work out-of-the-box:<br>

<br>Originally, I had something like this:<br><br>=A0// mylib/util.d:<br>=
=A0module mylib.util;<br>=A0class Foo { }<br><br>=A0// mylib/all.d:<br>=A0m=
odule mylib.all;<br>=A0public import mylib.util;<br><br>=A0// main.d:<br>=
=A0import mylib.all;<br>

=A0void main()<br>=A0{<br>=A0=A0=A0 auto f =3D new mylib.util.Foo;<br>=A0}<=
br><br>And this used to work. Now, I added a new file:<br><br>=A0// package=
.d<br>=A0module mylib;<br>=A0public import mylib.util;<br><br>And changed t=
he &#39;import&#39; in the main one:<br>

<br>=A0// main.d<br>=A0import mylib;<br><br>=A0void main()<br>=A0{<br>=A0=
=A0=A0 auto f =3D new mylib.util.Foo;<br>=A0}<br><br>Now, the compiler comp=
lains:<br><br>=A0main.d(5): Error: undefined identifier &#39;util&#39;<br>=
=A0main.d(5): Error: mylib.util.Foo is used as a type<br>

<br></div><div>[Using mylib.Foo instead of mylib.util.Foo works -- which ma=
kes sense when thnking about the use case of using package.d to split a lar=
ge package into smaller ones. ]<br></div><div><br><br>---------------------=
<br>

<br></div><div>EXAMPLE 2: Inconsistency with fully-qualified names<br><br>/=
/ mylib/util.d<br>module mylib.util;<br>class Foo { }<br><br>// mylib/packa=
ge.d<br>module mylib;<br>public import mylib.util;<br><br>// main.d<br>

import std.stdio;<br>import mylib;<br><br>void main()<br>{<br>=A0=A0 auto f=
 =3D new mylib.Foo;<br>=A0=A0 writefln(&quot;%s&quot;, <a href=3D"http://f.=
classinfo.name">f.classinfo.name</a>);<br>}<br><br>This prints &#39;mylib.u=
til.Foo&#39;. So far so good, that&#39;s the name I originally expected.<br=

<br>Then I try to instantiate a &#39;Foo&#39; using this very fully-qualifi= ed name the compiler told me:<br><br>=A0=A0 auto f =3D new mylib.util.Foo;<= br><br>And DMD doesn&#39;t like it anymore:<br><br>main.d(6): Error: undefi= ned identifier &#39;util&#39;<br> main.d(6): Error: mylib.util.Foo is used as a type<br><br></div><div>[This = looks very much like a bug for me. The name given by <a href=3D"http://clas= sinfo.name">classinfo.name</a> should be usable to instantiate a class, sho= uldn&#39;t it? ]<br> </div><div><br><br></div></div></div> --001a11369874d2fbca04f03dd98e--
Jan 18 2014