digitalmars.D - toString not working...
- Orgoton <orgoton mindless.com> Jan 15 2007
- Johan Granberg <lijat.meREM OVE.gmail.com> Jan 15 2007
- Orgoton <orgoton mindless.com> Jan 15 2007
- Marcin Kuszczak <aarti interia.pl> Jan 15 2007
- Ary Manzana <ary esperanto.org.ar> Jan 16 2007
- Daniel Keep <daniel.keep+lists gmail.com> Jan 16 2007
- Frits van Bommel <fvbommel REMwOVExCAPSs.nl> Jan 16 2007
- Leopold Walkling <leopold_walkling web.de> Jan 16 2007
- Daniel Giddings <dgiddings bigworldtech.com> Jan 16 2007
- Daniel Giddings <dgiddings bigworldtech.com> Jan 16 2007
I have ushort number=1092; char[] text="Some text " ~ number.toString(); it doesn't compile because presumably ushort has no property called "toString" yet on digitalmars.com/d it says that toString is defined on class "object" and every type inherits from this object. Any ideas?
Jan 15 2007
Orgoton wrote:I have ushort number=1092; char[] text="Some text " ~ number.toString(); it doesn't compile because presumably ushort has no property called "toString" yet on digitalmars.com/d it says that toString is defined on class "object" and every type inherits from this object. Any ideas?
Every type does not inherit from object, every class does. As ushort is not a class it has no such property. try this code instead; import std.string; toString(number);
Jan 15 2007
Ok, now I did import std.string; ushort number=1092; char[] text="Some text " ~ toString(number); and the compiler claimed that the number of arguments of Object.object.toString() did not match... So, I did char[] text="Some text " ~ std.string.toString(number); and it worked fine. Is this a compiler bug? I did not import object (as it is implicitly done). Or have I some weird compiler configuration?
Jan 15 2007
Orgoton wrote:Ok, now I did import std.string; ushort number=1092; char[] text="Some text " ~ toString(number); and the compiler claimed that the number of arguments of Object.object.toString() did not match... So, I did char[] text="Some text " ~ std.string.toString(number); and it worked fine. Is this a compiler bug? I did not import object (as it is implicitly done). Or have I some weird compiler configuration?
Probably you did: char[] text="Some text " ~ toString(number); in class method, so compiler thought you want to refer to Object's toString() method. You should escape current scope of class with '.' (dot). class Test { void method(int number) { char[] text="Some text " ~ .toString(number); } } Above code should work. -- Regards Marcin Kuszczak (Aarti_pl) ------------------------------------- Ask me why I believe in Jesus - http://zapytaj.dlajezusa.pl (en/pl) Doost (port of few Boost libraries) - http://www.dsource.org/projects/doost/ -------------------------------------
Jan 15 2007
Marcin Kuszczak escribió:Orgoton wrote:Ok, now I did import std.string; ushort number=1092; char[] text="Some text " ~ toString(number); and the compiler claimed that the number of arguments of Object.object.toString() did not match... So, I did char[] text="Some text " ~ std.string.toString(number); and it worked fine. Is this a compiler bug? I did not import object (as it is implicitly done). Or have I some weird compiler configuration?
Probably you did: char[] text="Some text " ~ toString(number); in class method, so compiler thought you want to refer to Object's toString() method. You should escape current scope of class with '.' (dot). class Test { void method(int number) { char[] text="Some text " ~ .toString(number); } } Above code should work.
That's not very intuitive :-( Can't this be fixed? toString methods should be easy to work since they are widely used.
Jan 16 2007
Ary Manzana wrote:That's not very intuitive :-( Can't this be fixed? toString methods should be easy to work since they are widely used.
I can think of two ways: 1) Allow for type extensions, or 2) require the use of "this" in front of all members -- hey, it works for Python! :3 The problem here is that if called from an object instance, "this.toString" shadows any global "toString"s. It's not so much a bug as a consequence of how D's lookup rules work. Personally, I think #1 above is the best long-term solution to this, as it allows you to more obviously disambiguate what you're trying to say. -- Daniel
Jan 16 2007
Daniel Keep wrote:Ary Manzana wrote:That's not very intuitive :-( Can't this be fixed? toString methods should be easy to work since they are widely used.
I can think of two ways: 1) Allow for type extensions, or
as a consequence of how D's lookup rules work. Personally, I think #1 above is the best long-term solution to this, as it allows you to more obviously disambiguate what you're trying to say.
I agree that would be a nice thing to have. And it can't really be that hard to implement; it's already done for arrays, why not for other types?
Jan 16 2007
Hello Marcin,Orgoton wrote:Ok, now I did import std.string; ushort number=1092; char[] text="Some text " ~ toString(number); and the compiler claimed that the number of arguments of Object.object.toString() did not match... So, I did char[] text="Some text " ~ std.string.toString(number); and it worked fine. Is this a compiler bug? I did not import object (as it is implicitly done). Or have I some weird compiler configuration?
char[] text="Some text " ~ toString(number); in class method, so compiler thought you want to refer to Object's toString() method. You should escape current scope of class with '.' (dot). class Test { void method(int number) { char[] text="Some text " ~ .toString(number); } } Above code should work.
This problem only occurs if you import specific modules (std.thread). So if you comment out that line, you can use toString without the dot, this isn't the wanted behavior, right?
Jan 16 2007
I've found use of format better in many cases as with tuple's the
compiler can get confused as to which toString variant to use (giving a
link error because its trying to compile a call in to toString(bool) on
a char[]), and it happily avoids any naming conflicts with the toString
of your class.
class Test {
void method(int number) {
char[] text="Some text " ~ format(number);
}
}
Marcin Kuszczak wrote:
Orgoton wrote:
Ok, now I did
import std.string;
ushort number=1092;
char[] text="Some text " ~ toString(number);
and the compiler claimed that the number of arguments of
Object.object.toString() did not match... So, I did
char[] text="Some text " ~ std.string.toString(number);
and it worked fine. Is this a compiler bug? I did not import object (as it
is implicitly done). Or have I some weird compiler configuration?
Probably you did:
char[] text="Some text " ~ toString(number);
in class method, so compiler thought you want to refer to Object's
toString() method.
You should escape current scope of class with '.' (dot).
class Test {
void method(int number) {
char[] text="Some text " ~ .toString(number);
}
}
Above code should work.
Jan 16 2007
I suppose I should give an example of the matching error:
import std.stdio;
import std.string;
struct S(T...)
{
static assert( T.length > 0 );
T fields;
char[] toString()
{
char[] result = "(" ~ .toString(fields[0]);
foreach( f; fields[1..$] )
result ~= "," ~ .toString(f);
return result ~ ")";
}
}
void main()
{
S!(char[],double) t;
writefln( t );
}
compiling gives:
t.d(12): function std.string.toString (bool) does not match parameter
types (char[])
t.d(8): Error: cannot implicitly convert expression
this._fields_field_0) of type char[] to char*
t.d(22): template instance t.S!(char[],double) error instantiating
but using format:
import std.stdio;
import std.string;
struct S(T...)
{
static assert( T.length > 0 );
T fields;
char[] toString()
{
char[] result = "(" ~ format(fields[0]);
foreach( f; fields[1..$] )
result ~= "," ~ format(f);
return result ~ ")";
}
}
void main()
{
S!(char[],double) t;
writefln( t );
}
compiles, and correctly gives:
(,nan)
Jan 16 2007









Frits van Bommel <fvbommel REMwOVExCAPSs.nl> 