www.digitalmars.com         C & C++   DMDScript  

D - puts(), toString method for primitives?

reply Jonathan Andrew <Jonathan_member pathlink.com> writes:
Hello everyone,
I noticed that to use puts() in D, the input had to be a null-terminated
C string, requiring toStringz(). I'm guessing that a full D version will be
present as phobos matures. I think puts (or the D equivalent) could replace
printf altogether and make D a lot easier for newbies like myself if primitive
types had a built in toString method, so to print out strings you could just 
call puts(mystring ~ someint.toString ~ somefloat.toString ...etc.), instead of
messing around with printf and worrying about %*.s and all that. The 
toString() function in phobos is only implemented for a few types right now,
unless I am missing something, so even if a D puts() was made part of the
standard library, it would still depend on itoa-like functions (which are
frustratingly never around!). Just a bit of nitpicking I suppose. Also, is there
a way to define new "pseudo-methods" for primitive types? That way if nobody
else wanted a .toString method for floats or whatever I could always just define
it for my own code. I tried looking in the spec, but couldn't find any mention
of it.

Thanks,
Jon 
Feb 19 2003
parent reply "Walter" <walter digitalmars.com> writes:
"Jonathan Andrew" <Jonathan_member pathlink.com> wrote in message
news:b31rlh$1fpv$1 digitaldaemon.com...
 Hello everyone,
 I noticed that to use puts() in D, the input had to be a null-terminated
 C string, requiring toStringz(). I'm guessing that a full D version will

 present as phobos matures. I think puts (or the D equivalent) could

 printf altogether and make D a lot easier for newbies like myself if

 types had a built in toString method, so to print out strings you could

 call puts(mystring ~ someint.toString ~ somefloat.toString ...etc.),

 messing around with printf and worrying about %*.s and all that. The
 toString() function in phobos is only implemented for a few types right

 unless I am missing something, so even if a D puts() was made part of the
 standard library, it would still depend on itoa-like functions (which are
 frustratingly never around!). Just a bit of nitpicking I suppose.

You're right, that needs to be done.
 Also, is there
 a way to define new "pseudo-methods" for primitive types? That way if

 else wanted a .toString method for floats or whatever I could always just

 it for my own code. I tried looking in the spec, but couldn't find any

 of it.

No, there isn't a way to do that.
Mar 01 2003
parent reply Marcelo Fontenele S Santos <msantos pobox.com> writes:
Walter wrote:
 "Jonathan Andrew" <Jonathan_member pathlink.com> wrote in message
 news:b31rlh$1fpv$1 digitaldaemon.com...
 Also, is there a way to define new "pseudo-methods" for primitive types? 
 That way if nobody else wanted a .toString method for floats or
 whatever I could always just defineit for my own code.
 I tried looking in the spec, but couldn't find any mention of it.

No, there isn't a way to do that.

In Ruby there is a way of extending an existing class or maybe even a primitive type but I'm not sure. I think you can have multiple declarations for a class and then ruby will add the methods into the class interface. class SomeClass def someMethod end end anObject = SomeClass.new anObject.someMethod anObject.anotherMethod # error class SomeClass def anotherMethod end end anObject.anotherMethod # ok anotherObject = SomeClass.new anotherObject.someMethod anotherObject.anotherMethod Are there any plans for something like it? -- Marcelo Fontenele S Santos <msantos pobox.com>
Mar 11 2003
parent Bill Cox <bill viasic.com> writes:
Marcelo Fontenele S Santos wrote:
 Walter wrote:
 
 "Jonathan Andrew" <Jonathan_member pathlink.com> wrote in message
 news:b31rlh$1fpv$1 digitaldaemon.com...

 Also, is there a way to define new "pseudo-methods" for primitive types? 


>> whatever I could always just defineit for my own code. >> I tried looking in the spec, but couldn't find any mention of it.
 No, there isn't a way to do that.

In Ruby there is a way of extending an existing class or maybe even a primitive type but I'm not sure. I think you can have multiple declarations for a class and then ruby will add the methods into the class interface. class SomeClass def someMethod end end anObject = SomeClass.new anObject.someMethod anObject.anotherMethod # error class SomeClass def anotherMethod end end anObject.anotherMethod # ok anotherObject = SomeClass.new anotherObject.someMethod anotherObject.anotherMethod Are there any plans for something like it?

There are no plans I'm aware of. However, I'd be for it, and I'm a very much a minimalist. However, I'd want any class extensions to be done at compile time. D isn't a good language for run-time dynamic stuff. Bill
Mar 12 2003