www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 2028] New: Can't create template class in module of same name

reply d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=2028

           Summary: Can't create template class in module of same name
           Product: D
           Version: 2.012
          Platform: PC
        OS/Version: Linux
            Status: NEW
          Severity: normal
          Priority: P2
         Component: DMD
        AssignedTo: bugzilla digitalmars.com
        ReportedBy: jlquinn us.ibm.com


I'm not sure this is a bug or a language limitation, but it feels like a bug...

I have 2 modules:

a.d:
class a(T) {}

b.d:
import a;
class b {
  a!(int) x;
}

When I try to compile this, I get the following error:

~/dmd/bin/dmd -c a.d b.d
b.d(4): template instance a is not a template declaration, it is a import
b.d(4): Error: a!(int) is used as a type
b.d(4): variable b.b.x voids have no value

Why can't I do this?


-- 
Apr 24 2008
next sibling parent "Jarrett Billingsley" <kb3ctd2 yahoo.com> writes:
<d-bugmail puremagic.com> wrote in message 
news:bug-2028-3 http.d.puremagic.com/issues/...
 http://d.puremagic.com/issues/show_bug.cgi?id=2028

           Summary: Can't create template class in module of same name
           Product: D
           Version: 2.012
          Platform: PC
        OS/Version: Linux
            Status: NEW
          Severity: normal
          Priority: P2
         Component: DMD
        AssignedTo: bugzilla digitalmars.com
        ReportedBy: jlquinn us.ibm.com


 I'm not sure this is a bug or a language limitation, but it feels like a 
 bug...

 I have 2 modules:

 a.d:
 class a(T) {}

 b.d:
 import a;
 class b {
  a!(int) x;
 }

 When I try to compile this, I get the following error:

 ~/dmd/bin/dmd -c a.d b.d
 b.d(4): template instance a is not a template declaration, it is a import
 b.d(4): Error: a!(int) is used as a type
 b.d(4): variable b.b.x voids have no value

 Why can't I do this?

I don't think it's a "bug" but just a sort of unintuitive bit of symbol lookup. In b.d, when you import a, it creates an entry in b's symbol table that maps from 'a' to 'module a'. It also creates entries for all the symbols inside 'module a', except for anything that conflicts. So when you refer to 'a' in 'module b', it refers to 'module a' and not 'class a'. This is only a problem if the class (or really any symbol) name is the same as the *top-level name* of a module. So if you instead had "mylib.a" as the name of the module, accessing 'class a' would not be a problem. Of course, then accessing a symbol named "mylib" would.
Apr 24 2008
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=2028





------- Comment #1 from webmaster villagersonline.com  2008-04-24 15:47 -------
I don't know whether to call this a bug or a language design issue, but I know
the workaround: specify the full name

Use
    a.a
instead of
    a


-- 
Apr 24 2008
prev sibling next sibling parent reply d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=2028





------- Comment #2 from wbaxter gmail.com  2008-04-24 17:04 -------
(In reply to comment #1)
 I don't know whether to call this a bug or a language design issue, but I know
 the workaround: specify the full name
 
 Use
     a.a
 instead of
     a
 

I think this can also be avoided by having the module in a package. Then DMD seems not to get confused. I've not totally understood the specifics, it seems modules and other symbols conflict in ways that package names don't. So I think if you put a.d into an 'a' directory, make it's module statement be "module a.a;" then do import a.a; then you won't get the conflict. Can anyone confirm this? Don't have time at the moment... --
Apr 24 2008
parent "Jarrett Billingsley" <kb3ctd2 yahoo.com> writes:
<d-bugmail puremagic.com> wrote in message 
news:fur052$309q$1 digitalmars.com...
 http://d.puremagic.com/issues/show_bug.cgi?id=2028





 ------- Comment #2 from wbaxter gmail.com  2008-04-24 17:04 -------
 (In reply to comment #1)
 I don't know whether to call this a bug or a language design issue, but I 
 know
 the workaround: specify the full name

 Use
     a.a
 instead of
     a

I think this can also be avoided by having the module in a package. Then DMD seems not to get confused. I've not totally understood the specifics, it seems modules and other symbols conflict in ways that package names don't. So I think if you put a.d into an 'a' directory, make it's module statement be "module a.a;" then do import a.a; then you won't get the conflict. Can anyone confirm this? Don't have time at the moment...

That wouldn't work either, since the top-level name is also a. It's that modules and packages have a higher precedence than the symbols within them.
Apr 24 2008
prev sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=2028





------- Comment #3 from wbaxter gmail.com  2008-04-24 18:23 -------
(In reply to comment #2)
 (In reply to comment #1)
 I don't know whether to call this a bug or a language design issue, but I know
 the workaround: specify the full name
 
 Use
     a.a
 instead of
     a
 

I think this can also be avoided by having the module in a package. Then DMD seems not to get confused. I've not totally understood the specifics, it seems modules and other symbols conflict in ways that package names don't. So I think if you put a.d into an 'a' directory, make it's module statement be "module a.a;" then do import a.a; then you won't get the conflict. Can anyone confirm this? Don't have time at the moment...

Jarrett Billingsley wrote:
That wouldn't work either, since the top-level name is also a.  It's that 

Ah, ok. So I guess when I've gotten rid of such problems in the past by putting the offending module in a package, it worked because I named the package something that didn't conflict. But this still does point out the issue with namespace pollution that arises when using top-level, no-package modules. Seems like that should be a general rule of thumb for D programming -- "Effective D": Rule #1: always put your classes in a package. --
Apr 24 2008