Thread overview | ||||||||
---|---|---|---|---|---|---|---|---|
|
March 22, 2005 String concat ~ with auto toString() feature? | ||||
---|---|---|---|---|
| ||||
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]~ "', line #"~toString(i+1)~" requires"~ 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 |
March 22, 2005 Re: String concat ~ with auto toString() feature? | ||||
---|---|---|---|---|
| ||||
Posted in reply to AEon | 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]~ > "', line #"~toString(i+1)~" requires"~ > 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( "File '%s', section '%s', line #%d requires %d, not %d!", cfgPathFile, cfg_Sections[inSec], i+1, cfg_Quote_Counts[inSec]), qcount )); -- Derek Parnell Melbourne, Australia 22/03/2005 11:54:48 PM |
March 22, 2005 Re: String concat ~ with auto toString() feature? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Derek Parnell | In article <1rx44m7eaue9a$.11bm51tuirl2t$.dlg@40tude.net>, Derek Parnell says... > >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]~ >> "', line #"~toString(i+1)~" requires"~ >> 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( > "File '%s', section '%s', line #%d requires %d, not %d!", > cfgPathFile, > cfg_Sections[inSec], > i+1, > cfg_Quote_Counts[inSec]), > qcount > )); Interesting example, I had been wondering how format() is actually used. Thanx. AEon |
March 22, 2005 Re: String concat ~ with auto toString() feature? | ||||
---|---|---|---|---|
| ||||
Posted in reply to AEon | 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]~
> "', line #"~toString(i+1)~" requires"~
> 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
|
March 22, 2005 Re: String concat ~ with auto toString() feature? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Regan Heath | Regan Heath says... >> 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. Right. >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 |
March 22, 2005 Re: String concat ~ with auto toString() feature? | ||||
---|---|---|---|---|
| ||||
Posted in reply to AEon | 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
|
Copyright © 1999-2021 by the D Language Foundation