www.digitalmars.com         C & C++   DMDScript  

D - toString and templates

reply J Anderson <REMOVEanderson badmama.com.au> writes:
How would I write a template print statement that would print out either 
an object or and primitive type?

template printT(T)
{
    void printt(T type)
    {
       obj.printf(obj.toString());  //Works for objects but not primitives
    }
}


template printT(T)
{
    void printt(T type)
    {
       obj.printf(toString(obj));  //Works for *some* primitives but not 
objects
    }
}

Anyway, I think for cases like this, there should be a function defined 
in phobos. ie
char [] toString(Object obj) {return obj.toString); //Should be in object.d

Is there a toString(float f)?  I think there should be a toString for 
all primitives in phobos.  This way you can easily use templates with 
these methods.

Other possibilities are:
toHash
ClassInfo  (every privative could have a corresponding classinfo)

Anyway IMHO, struct, classes and primitives should be able to be treated 
much the same way, making templates more versitile.

-Anderson
Dec 16 2003
next sibling parent reply "Charles" <sanders-consulting comcast.net> writes:
This brings up a good point.  I think before we add any new features we need
to fix the inconsitinicies in the language.  I think toString should be a
property for primitive types, Ill also take remove as a property for arrays.
I know there are more that im missing, and it sounds trivial but I think
these changes will make the language much stronger.

C

"J Anderson" <REMOVEanderson badmama.com.au> wrote in message
news:brng6c$1h8e$1 digitaldaemon.com...
 How would I write a template print statement that would print out either
 an object or and primitive type?

 template printT(T)
 {
     void printt(T type)
     {
        obj.printf(obj.toString());  //Works for objects but not primitives
     }
 }


 template printT(T)
 {
     void printt(T type)
     {
        obj.printf(toString(obj));  //Works for *some* primitives but not
 objects
     }
 }

 Anyway, I think for cases like this, there should be a function defined
 in phobos. ie
 char [] toString(Object obj) {return obj.toString); //Should be in
object.d
 Is there a toString(float f)?  I think there should be a toString for
 all primitives in phobos.  This way you can easily use templates with
 these methods.

 Other possibilities are:
 toHash
 ClassInfo  (every privative could have a corresponding classinfo)

 Anyway IMHO, struct, classes and primitives should be able to be treated
 much the same way, making templates more versitile.

 -Anderson
Dec 16 2003
parent reply J Anderson <REMOVEanderson badmama.com.au> writes:
Charles wrote:

This brings up a good point.  I think before we add any new features we need
to fix the inconsitinicies in the language.  I think toString should be a
property for primitive types, Ill also take remove as a property for arrays.
I know there are more that im missing, and it sounds trivial but I think
these changes will make the language much stronger.

C
  
I do as well. I was only trying to suggest, the easy -> non-language changing method, to increase the liklyhood of it being fixed.
"J Anderson" <REMOVEanderson badmama.com.au> wrote in message
news:brng6c$1h8e$1 digitaldaemon.com...
  

How would I write a template print statement that would print out either
an object or and primitive type?

template printT(T)
{
    void printt(T type)
    {
       obj.printf(obj.toString());  //Works for objects but not primitives
    }
}


template printT(T)
{
    void printt(T type)
    {
       obj.printf(toString(obj));  //Works for *some* primitives but not
objects
    }
}

Anyway, I think for cases like this, there should be a function defined
in phobos. ie
char [] toString(Object obj) {return obj.toString()); //Should be in
    
object.d
Is there a toString(float f)?  I think there should be a toString for
all primitives in phobos.  This way you can easily use templates with
these methods.

Other possibilities are:
toHash
ClassInfo  (every privative could have a corresponding classinfo)

Anyway IMHO, struct, classes and primitives should be able to be treated
much the same way, making templates more versitile.

-Anderson


    
Dec 16 2003
parent "Matthew Wilson" <matthew.hat stlsoft.dot.org> writes:
I think that, wherever possible, D should follow C++'s philosophy of adding
library features rather than language features.

The point is, however, that the point at which we (or Walter, anyway) deems
the appropriate intersection need (should!) not be the same as it is for
C++.

Matthew

(Sorry if that is unintelligible. My blurred contact lens is reflecting a
blurring of the grey matter behind it ... bibble bibble)

"J Anderson" <REMOVEanderson badmama.com.au> wrote in message
news:brnjup$1mja$2 digitaldaemon.com...
 Charles wrote:

This brings up a good point.  I think before we add any new features we
need
to fix the inconsitinicies in the language.  I think toString should be a
property for primitive types, Ill also take remove as a property for
arrays.
I know there are more that im missing, and it sounds trivial but I think
these changes will make the language much stronger.

C
I do as well. I was only trying to suggest, the easy -> non-language changing method, to increase the liklyhood of it being fixed.
"J Anderson" <REMOVEanderson badmama.com.au> wrote in message
news:brng6c$1h8e$1 digitaldaemon.com...


How would I write a template print statement that would print out either
an object or and primitive type?

template printT(T)
{
    void printt(T type)
    {
       obj.printf(obj.toString());  //Works for objects but not
primitives
    }
}


template printT(T)
{
    void printt(T type)
    {
       obj.printf(toString(obj));  //Works for *some* primitives but not
objects
    }
}

Anyway, I think for cases like this, there should be a function defined
in phobos. ie
char [] toString(Object obj) {return obj.toString()); //Should be in
object.d
Is there a toString(float f)?  I think there should be a toString for
all primitives in phobos.  This way you can easily use templates with
these methods.

Other possibilities are:
toHash
ClassInfo  (every privative could have a corresponding classinfo)

Anyway IMHO, struct, classes and primitives should be able to be treated
much the same way, making templates more versitile.

-Anderson
Dec 16 2003
prev sibling parent reply "Matthew Wilson" <matthew.hat stlsoft.dot.org> writes:
Yes, there should be a set of standard shims, as these things are called (by
me, anyway).

In a moment of modesty, may I suggest you read
http://www.cuj.com/documents/s=8681/cuj0308wilson/.

Cheers

Matthew


"J Anderson" <REMOVEanderson badmama.com.au> wrote in message
news:brng6c$1h8e$1 digitaldaemon.com...
 How would I write a template print statement that would print out either
 an object or and primitive type?

 template printT(T)
 {
     void printt(T type)
     {
        obj.printf(obj.toString());  //Works for objects but not primitives
     }
 }


 template printT(T)
 {
     void printt(T type)
     {
        obj.printf(toString(obj));  //Works for *some* primitives but not
 objects
     }
 }

 Anyway, I think for cases like this, there should be a function defined
 in phobos. ie
 char [] toString(Object obj) {return obj.toString); //Should be in
object.d
 Is there a toString(float f)?  I think there should be a toString for
 all primitives in phobos.  This way you can easily use templates with
 these methods.

 Other possibilities are:
 toHash
 ClassInfo  (every privative could have a corresponding classinfo)

 Anyway IMHO, struct, classes and primitives should be able to be treated
 much the same way, making templates more versitile.

 -Anderson
Dec 16 2003
parent reply J Anderson <REMOVEanderson badmama.com.au> writes:
Matthew Wilson wrote:

Yes, there should be a set of standard shims, as these things are called (by
me, anyway).

In a moment of modesty, may I suggest you read
http://www.cuj.com/documents/s=8681/cuj0308wilson/.

Cheers

Matthew

  
Interesting.
"J Anderson" <REMOVEanderson badmama.com.au> wrote in message
news:brng6c$1h8e$1 digitaldaemon.com...
  

How would I write a template print statement that would print out either
an object or and primitive type?

template printT(T)
{
    void printt(T type)
    {
       obj.printf(obj.toString());  //Works for objects but not primitives
    }
}


template printT(T)
{
    void printt(T type)
    {
       obj.printf(toString(obj));  //Works for *some* primitives but not
objects
    }
}

Anyway, I think for cases like this, there should be a function defined
in phobos. ie
char [] toString(Object obj) {return obj.toString); //Should be in
    
object.d
Is there a toString(float f)?  I think there should be a toString for
all primitives in phobos.  This way you can easily use templates with
these methods.

Other possibilities are:
toHash
ClassInfo  (every privative could have a corresponding classinfo)

Anyway IMHO, struct, classes and primitives should be able to be treated
much the same way, making templates more versitile.

-Anderson


    
Dec 16 2003
parent "Matthew Wilson" <matthew.hat stlsoft.dot.org> writes:
 Matthew Wilson wrote:

Yes, there should be a set of standard shims, as these things are called
(by
me, anyway).

In a moment of modesty, may I suggest you read
http://www.cuj.com/documents/s=8681/cuj0308wilson/.

Cheers

Matthew
Interesting.
The article, or my singular inability to demure? ;)
Dec 16 2003