www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - BUG?

reply Tomás Rossi <Tomás_member pathlink.com> writes:
With the latest DMD compiler for Windows this is what happens:

--in file Person.d--
module Person;

private import std.string;

alias char[] string;

class Person(T)
{
protected:
uint m_age;
string m_name;
T m_other_property;

public:
// Default constructor.
this(string name, uint age)
{
m_name = name;
m_age = age;
}

string toString()
{
return m_name ~": "~ .toString(m_age) ~","~
toString(m_other_property); 
}
} 

--in file test1.d--

private import std.stdio, std.process;
import Person;

int main()
{
int ret_code;
try	
{
Person!(int) new_person = new Person!(int)("George", 16);
writefln(new_person.toString());
}
catch (Exception ex)
{
writefln("Unknown Exception: %s", ex.toString());
ret_code = 1;
}
finally
{
std.process.system("pause");
}
return ret_code;
}

DMD throws:
ERROR(9): template instance Person is not a template declaration, it is a import
ERROR(9): Person!(int) used as a type
.. etc ...

If i comment the "import Person;" and copy-paste the class code into the main
file, everything compiles fine.

I don't understand why?!?! 

Tom
Oct 31 2005
next sibling parent reply Derek Parnell <derek psych.ward> writes:
On Tue, 1 Nov 2005 04:58:56 +0000 (UTC), Tomás Rossi wrote:

 With the latest DMD compiler for Windows this is what happens:
 
 --in file Person.d--
 module Person;
 
 private import std.string;
 
 alias char[] string;
 
 class Person(T)
 {
 protected:
 uint m_age;
 string m_name;
 T m_other_property;
 
 public:
 // Default constructor.
 this(string name, uint age)
 {
 m_name = name;
 m_age = age;
 }
 
 string toString()
 {
 return m_name ~": "~ .toString(m_age) ~","~
 toString(m_other_property); 
 }
 } 
 
 --in file test1.d--
 
 private import std.stdio, std.process;
 import Person;
 
 int main()
 {
 int ret_code;
 try	
 {
 Person!(int) new_person = new Person!(int)("George", 16);
 writefln(new_person.toString());
 }
 catch (Exception ex)
 {
 writefln("Unknown Exception: %s", ex.toString());
 ret_code = 1;
 }
 finally
 {
 std.process.system("pause");
 }
 return ret_code;
 }
 
 DMD throws:
 ERROR(9): template instance Person is not a template declaration, it is a
import
 ERROR(9): Person!(int) used as a type
 .. etc ...
 
 If i comment the "import Person;" and copy-paste the class code into the main
 file, everything compiles fine.
 
 I don't understand why?!?! 
 
 Tom

The main problem is that you need to remove the ambiguous references. There are two issues. (1) In Person.d change the toString() to this ... string toString() { return m_name ~": "~ .toString(m_age) ~","~ std.string.toString(m_other_property); } (2) In test1.d change the declaration to ... Person.Person!(int) new_person = new Person.Person!(int)("George", 16); -- Derek Parnell Melbourne, Australia 1/11/2005 6:37:58 PM
Oct 31 2005
next sibling parent "Regan Heath" <regan netwin.co.nz> writes:
On Tue, 1 Nov 2005 18:41:02 +1100, Derek Parnell <derek psych.ward> wrote:
 On Tue, 1 Nov 2005 04:58:56 +0000 (UTC), Tomás Rossi wrote:

 With the latest DMD compiler for Windows this is what happens:

 --in file Person.d--
 module Person;

 private import std.string;

 alias char[] string;

 class Person(T)
 {
 protected:
 uint m_age;
 string m_name;
 T m_other_property;

 public:
 // Default constructor.
 this(string name, uint age)
 {
 m_name = name;
 m_age = age;
 }

 string toString()
 {
 return m_name ~": "~ .toString(m_age) ~","~
 toString(m_other_property);
 }
 }

 --in file test1.d--

 private import std.stdio, std.process;
 import Person;

 int main()
 {
 int ret_code;
 try	
 {
 Person!(int) new_person = new Person!(int)("George", 16);
 writefln(new_person.toString());
 }
 catch (Exception ex)
 {
 writefln("Unknown Exception: %s", ex.toString());
 ret_code = 1;
 }
 finally
 {
 std.process.system("pause");
 }
 return ret_code;
 }

 DMD throws:
 ERROR(9): template instance Person is not a template declaration, it is  
 a import
 ERROR(9): Person!(int) used as a type
 .. etc ...

 If i comment the "import Person;" and copy-paste the class code into  
 the main
 file, everything compiles fine.

 I don't understand why?!?!

 Tom

The main problem is that you need to remove the ambiguous references. There are two issues. (1) In Person.d change the toString() to this ... string toString() { return m_name ~": "~ .toString(m_age) ~","~ std.string.toString(m_other_property); } (2) In test1.d change the declaration to ... Person.Person!(int) new_person = new Person.Person!(int)("George", 16);

In addition, the style guide: http://www.digitalmars.com/d/dstyle.html would have you change: "Person.d" to "person.d" "module Person" to "module person" "import Person" to "import person" ..etc.. This would have prevented the ambiguous references from occuring. Regan
Nov 01 2005
prev sibling parent reply Tomás Rossi <Tomás_member pathlink.com> writes:
In article <1t0aqb9bev97m$.1btnl6miit6v4$.dlg 40tude.net>, Derek Parnell says...
On Tue, 1 Nov 2005 04:58:56 +0000 (UTC), Tomás Rossi wrote:

 With the latest DMD compiler for Windows this is what happens:
 
 --in file Person.d--
 module Person;
 
 private import std.string;
 
 alias char[] string;
 
 class Person(T)
 {
 protected:
 uint m_age;
 string m_name;
 T m_other_property;
 
 public:
 // Default constructor.
 this(string name, uint age)
 {
 m_name = name;
 m_age = age;
 }
 
 string toString()
 {
 return m_name ~": "~ .toString(m_age) ~","~
 toString(m_other_property); 
 }
 } 
 
 --in file test1.d--
 
 private import std.stdio, std.process;
 import Person;
 
 int main()
 {
 int ret_code;
 try	
 {
 Person!(int) new_person = new Person!(int)("George", 16);
 writefln(new_person.toString());
 }
 catch (Exception ex)
 {
 writefln("Unknown Exception: %s", ex.toString());
 ret_code = 1;
 }
 finally
 {
 std.process.system("pause");
 }
 return ret_code;
 }
 
 DMD throws:
 ERROR(9): template instance Person is not a template declaration, it is a
import
 ERROR(9): Person!(int) used as a type
 .. etc ...
 
 If i comment the "import Person;" and copy-paste the class code into the main
 file, everything compiles fine.
 
 I don't understand why?!?! 
 
 Tom

The main problem is that you need to remove the ambiguous references. There are two issues. (1) In Person.d change the toString() to this ... string toString() { return m_name ~": "~ .toString(m_age) ~","~ std.string.toString(m_other_property); }

OK, this was a cpoy paste mistake, since in my code i had it .toString(...blah)
(2) In test1.d change the declaration to ... 

Person.Person!(int) new_person = new Person.Person!(int)("George", 16);

This is fine but, i've missed to mention the real problem (sorry): If I remove al the generic (template) things for example: class Person(T) --> class Person remove the T member and the toString of T member. and i instantiate like this: Person new_person = new Person("George", 16); Everything goes just fine. There are no Module-Class ambiguity (the class remains still in Person.d module). Summarizing, the same code but with a template parameter goes to compiler errors and when removing the template parameter (and all the related code to T), the error just come into light. Why?
-- 
Derek Parnell
Melbourne, Australia
1/11/2005 6:37:58 PM

Tom SFME (Sorry for my English)
Nov 01 2005
parent Derek Parnell <derek psych.ward> writes:
On Tue, 1 Nov 2005 10:52:57 +0000 (UTC), Tomás Rossi wrote:

[snip]

 Summarizing, the same code but with a template parameter goes to compiler
errors
 and when removing the template parameter (and all the related code to T), the
 error just come into light. Why?

My guess is when DMD sees "Person!(int)", it interprets "Person" as the module name and wonders why you are trying to use the template instantiation syntax on the module. Thus when you remove that stuff, it goes to another level of analysis and corrects the interpretation of "Person" to a class name and now it works. -- Derek Parnell Melbourne, Australia 2/11/2005 12:00:53 AM
Nov 01 2005
prev sibling parent "John C" <johnch_atms hotmail.com> writes:
"Tomás Rossi" <Tomás_member pathlink.com> wrote in message 
news:dk6smg$2b64$1 digitaldaemon.com...
 With the latest DMD compiler for Windows this is what happens:

 --in file Person.d--
 module Person;

The recommendation is to use lower case module names.
 private import std.string;

 alias char[] string;

 class Person(T)
 {
 protected:
 uint m_age;
 string m_name;
 T m_other_property;

 public:
 // Default constructor.
 this(string name, uint age)
 {
 m_name = name;
 m_age = age;
 }

 string toString()
 {
 return m_name ~": "~ .toString(m_age) ~","~
 toString(m_other_property);
 }
 }

D sees 'toString(m_other_property)' as being ambiguous (it tries to match your Person's toString). Just use the global scope operator as you did with '.toString(m_age)'.
 --in file test1.d--

 private import std.stdio, std.process;
 import Person;

 int main()
 {
 int ret_code;
 try
 {
 Person!(int) new_person = new Person!(int)("George", 16);
 writefln(new_person.toString());
 }
 catch (Exception ex)
 {
 writefln("Unknown Exception: %s", ex.toString());
 ret_code = 1;
 }
 finally
 {
 std.process.system("pause");
 }
 return ret_code;
 }

 DMD throws:
 ERROR(9): template instance Person is not a template declaration, it is a 
 import
 ERROR(9): Person!(int) used as a type

Follow the advise above and it will compile fine. Think of modules as namespaces. In C++, you wouldn't name a namespace "Person" and have a class with the same name.
 .. etc ...

 If i comment the "import Person;" and copy-paste the class code into the 
 main
 file, everything compiles fine.

 I don't understand why?!?!

 Tom 

Nov 01 2005