www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - A few BUGS to squish

reply Knaar <Knaar_member pathlink.com> writes:
Hi!
I love the language. I would like to see the std. libraries 100% object
oriented, but that's mild considering it fixes a heck of a lot of problems with
C and C++.

I'm trying to do some real coding with D and I've found some annoying bugs.

The first thing I noticed is problems with the toString() method. In std.string
there are a great number of methods to convert base types to char[]. Here is an
example of the problem I had:

module math.Vector;
import std.string;

class Vector {
private:
double x, y;
public:
char[] toString() {
return "("~toString(x)~","~toString(y)~")";
}
}

Now, immediately you'll notice that this snippet does not compile, even though,
intuitively it _should_ compile. The problem appears to be that the compiler
does not look outside of Vector for toString() definitions. Java, of course,
handles this by not allowing global methods whatsoever. But since D has global
methods, D must search _both_ the local scope, and recursively check all parent
scopes, so in this case it must check the scope of toString(), then Vector, then
math.Vector, then the default module scope, then all imported scopes including
std.string. I worked around this bug by using the fully qualified name
std.string.toString(double).

The second bug is demonstrated in the following code:

module math.Vector;
import std.math;

Boom... not even these two lines compile. I don't even have to do anything more,
it says "module math is multiply defined". I need to use sqrt() in order to
calculate the distance between two Vectors, but it looks like I can't call the
package I'm using math, otherwise I can't import std.math. Kinda sucky,
considering the two names, intuitively, shouldn't conflict at all.

-Ryan
May 16 2004
next sibling parent reply J Anderson <REMOVEanderson badmama.com.au> writes:
Knaar wrote:

 Hi!
 I love the language. I would like to see the std. libraries 100% object
 oriented, but that's mild considering it fixes a heck of a lot of 
 problems with
 C and C++.

 I'm trying to do some real coding with D and I've found some annoying 
 bugs.

 The first thing I noticed is problems with the toString() method. In 
 std.string
 there are a great number of methods to convert base types to char[]. 
 Here is an
 example of the problem I had:

 module math.Vector;
 import std.string;

 class Vector {
 private:
 double x, y;
 public:
 char[] toString() {
 return "("~toString(x)~","~toString(y)~")";
 }
 }
  
Put a dot in front of toString to get access to global scope. class Vector { private: double x, y; public: char[] toString() { return "("~.toString(x)~","~.toString(y)~")"; } }
 <snip>
  
 The second bug is demonstrated in the following code:

 module math.Vector;
 import std.math;

 Boom... not even these two lines compile. I don't even have to do 
 anything more,
 it says "module math is multiply defined". I need to use sqrt() in 
 order to
 calculate the distance between two Vectors, but it looks like I can't 
 call the
 package I'm using math, otherwise I can't import std.math. Kinda sucky,
 considering the two names, intuitively, shouldn't conflict at all.

 -Ryan
  
Actually its the first line that conflicts the second line has nothing to do with it. The reason is because there is a module math; in phobos, so that namespace is already used. D doesn't allow you to have a module name the same name as a folder. You could always create your own namespace ie: module Knaar.math.Vector; PS - there is a D bugs newsgroup. -- -Anderson: http://badmama.com.au/~anderson/
May 16 2004
parent reply Knaar <Knaar_member pathlink.com> writes:
Thanks for the reply. I guess that makes sense. I'll just stuff all my code in
another parent directory. Sorry about the bug report here in this newsgroup.
I'll try to find the bugs forum.

-Knaar
May 16 2004
parent J Anderson <REMOVEanderson badmama.com.au> writes:
Knaar wrote:

Thanks for the reply. I guess that makes sense. I'll just stuff all my code in
another parent directory. Sorry about the bug report here in this newsgroup.
I'll try to find the bugs forum.

-Knaar
  
Are you using a newsgroup program (which I recommend) or the webpage? Anyway heres the web-based D forums: http://www.digitalmars.com/drn-bin/wwwnews?digitalmars.* -- -Anderson: http://badmama.com.au/~anderson/
May 16 2004
prev sibling parent Andrew Edwards <ridimz_at yahoo.dot.com> writes:
Knaar wrote:
 Hi!
 I love the language. I would like to see the std. libraries 100% object
 oriented, but that's mild considering it fixes a heck of a lot of problems with
 C and C++.
 
 I'm trying to do some real coding with D and I've found some annoying bugs.
 
 The first thing I noticed is problems with the toString() method. In std.string
 there are a great number of methods to convert base types to char[]. Here is an
 example of the problem I had:
 
 module math.Vector;
 import std.string;
 
 class Vector {
 private:
 double x, y;
 public:
 char[] toString() {
 return "("~toString(x)~","~toString(y)~")";
 }
 }
 
 Now, immediately you'll notice that this snippet does not compile, even though,
 intuitively it _should_ compile. The problem appears to be that the compiler
 does not look outside of Vector for toString() definitions.
It works like a charm if you do this instead: return "("~.toString(x)~","~.toString(y)~")"; note the "." Java, of course,
 handles this by not allowing global methods whatsoever. But since D has global
 methods, D must search _both_ the local scope, and recursively check all parent
 scopes, so in this case it must check the scope of toString(), then Vector,
then
 math.Vector, then the default module scope, then all imported scopes including
 std.string. I worked around this bug by using the fully qualified name
 std.string.toString(double).
 
 The second bug is demonstrated in the following code:
 
 module math.Vector;
 import std.math;
 
 Boom... not even these two lines compile. I don't even have to do anything
more,
 it says "module math is multiply defined". I need to use sqrt() in order to
 calculate the distance between two Vectors, but it looks like I can't call the
 package I'm using math, otherwise I can't import std.math. Kinda sucky,
 considering the two names, intuitively, shouldn't conflict at all.
 
 -Ryan
 
 
May 16 2004