digitalmars.D.learn - String concat ~ with auto toString() feature?
- AEon (17/17) Mar 22 2005 I have been using ~ in many string functions, but was wondering if D cou...
- Derek Parnell (14/32) Mar 22 2005 I use this style ...
- AEon (3/31) Mar 22 2005 Interesting example, I had been wondering how format() is actually used....
- Regan Heath (85/105) Mar 22 2005 What you're essentially asking for is the ability to define opCat
- AEon (8/16) Mar 22 2005 Nice example.
- Chris Sauls (7/11) Mar 22 2005 The ~ operator works with any array, or even with any struct or class
I have been using ~ in many string functions, but was wondering if D counld not automatrically "implicitly" convert any non-strings to strings in cases like this: Warning(warn, "File '"~cfgPathFile~"', section '"~cfg_Sections[inSec]~ toString(cfg_Quote_Counts[inSec])~" \", not "~ toString(qcount)~"!"); with void Warning(char[] from, char[] message) { printf("\n WARNING ( %.*s ) : \n %.*s\n\n", from, message); } I.e. qcount is a int, when surrounded by (string) ~, could it not be clear that the user would want to have qcount converted to toString(qcount)? Just wondering since in the above case AFAICT ~ is for string operations only, so there should not be any ambiguity? AEon
Mar 22 2005
On Tue, 22 Mar 2005 12:39:09 +0000 (UTC), AEon wrote:I have been using ~ in many string functions, but was wondering if D counld not automatrically "implicitly" convert any non-strings to strings in cases like this: Warning(warn, "File '"~cfgPathFile~"', section '"~cfg_Sections[inSec]~ toString(cfg_Quote_Counts[inSec])~" \", not "~ toString(qcount)~"!"); with void Warning(char[] from, char[] message) { printf("\n WARNING ( %.*s ) : \n %.*s\n\n", from, message); } I.e. qcount is a int, when surrounded by (string) ~, could it not be clear that the user would want to have qcount converted to toString(qcount)?I use this style ... Warning(warn, std.string.format( cfgPathFile, cfg_Sections[inSec], i+1, cfg_Quote_Counts[inSec]), qcount )); -- Derek Parnell Melbourne, Australia 22/03/2005 11:54:48 PM
Mar 22 2005
In article <1rx44m7eaue9a$.11bm51tuirl2t$.dlg 40tude.net>, Derek Parnell says...On Tue, 22 Mar 2005 12:39:09 +0000 (UTC), AEon wrote:Interesting example, I had been wondering how format() is actually used. Thanx. AEonI have been using ~ in many string functions, but was wondering if D counld not automatrically "implicitly" convert any non-strings to strings in cases like this: Warning(warn, "File '"~cfgPathFile~"', section '"~cfg_Sections[inSec]~ toString(cfg_Quote_Counts[inSec])~" \", not "~ toString(qcount)~"!"); with void Warning(char[] from, char[] message) { printf("\n WARNING ( %.*s ) : \n %.*s\n\n", from, message); } I.e. qcount is a int, when surrounded by (string) ~, could it not be clear that the user would want to have qcount converted to toString(qcount)?I use this style ... Warning(warn, std.string.format( cfgPathFile, cfg_Sections[inSec], i+1, cfg_Quote_Counts[inSec]), qcount ));
Mar 22 2005
On Tue, 22 Mar 2005 12:39:09 +0000 (UTC), AEon <AEon_member pathlink.com> wrote:I have been using ~ in many string functions, but was wondering if D counld not automatrically "implicitly" convert any non-strings to strings in cases like this: Warning(warn, "File '"~cfgPathFile~"', section '"~cfg_Sections[inSec]~ toString(cfg_Quote_Counts[inSec])~" \", not "~ toString(qcount)~"!"); with void Warning(char[] from, char[] message) { printf("\n WARNING ( %.*s ) : \n %.*s\n\n", from, message); } I.e. qcount is a int, when surrounded by (string) ~, could it not be clear that the user would want to have qcount converted to toString(qcount)? Just wondering since in the above case AFAICT ~ is for string operations only, so there should not be any ambiguity?What you're essentially asking for is the ability to define opCat operators for the char[] built-in type. To see what I mean here is some example code, pretend "String" is actually the built-in char[] type. The built in char type already has all the methods below (or their equivalent) except the opCat and opCatAssign operators for int and float, which is what you want to add. import std.string; import std.stdio; class String { public: //built-in char[] already has methods similar/equivalent to these this() { } this(String s) { data = s.data.dup; } this(char[] s) { data = s.dup; } String opCat(char[] v) { String s = new String(this); s ~= v; return s; } String opCatAssign(char[] v) { int index = data.length; data.length = data.length + v.length; data[index..data.length] = v[]; return this; } char[] toString() { return data; } //these methods are new String opCat(int v) { String s = new String(this); s ~= v; return s; } String opCatAssign(int v) { data ~= std.string.toString(v); return this; } String opCat(float v) { String s = new String(this); s ~= v; return s; } String opCatAssign(float v) { data ~= std.string.toString(v); return this; } private: char[] data; } void main() { String s = new String(); s = s ~ 5 ~ "regan" ~ 4.5; //calls opCat(int) opCat(char[]) and then opCat(float) writefln(s); //calls the toString method. } Regan
Mar 22 2005
Regan Heath says...Right.Just wondering since in the above case AFAICT ~ is for string operations only, so there should not be any ambiguity?What you're essentially asking for is the ability to define opCat operators for the char[] built-in type.To see what I mean here is some example code, pretend "String" is actually the built-in char[] type. The built in char type already has all the methods below (or their equivalent) except the opCat and opCatAssign operators for int and float, which is what you want to add.Nice example. So I am basically asking it would make sense to add the ~ operator for int/float for char[]? And if Walter would considder it, find it useful? (I don't really need it, this is more of a possible feature suggestion). AEon
Mar 22 2005
The ~ operator works with any array, or even with any struct or class with defines an opCat overload, so it might not be so easy. Plus, would this mean DMD would auto-import std.string to get the toString() functions? I could possibly understand it with classes though, since they should define their own .toString() method. -- Chris Sauls AEon wrote:Just wondering since in the above case AFAICT ~ is for string operations only, so there should not be any ambiguity? AEon
Mar 22 2005