Thread overview
string composition
Apr 28, 2005
B.G.
Apr 28, 2005
Andrew Fedoniouk
Apr 28, 2005
B.G.
Apr 28, 2005
Derek Parnell
April 28, 2005
Hi!

I was trying to port a C++ project to D.
I have many inplace string manipulations like this:

func("unexpected symbol: " + c);

where c may be any base type like char, int, etc.

In java those operations are part of the language
(this solution works perfectly with java language as such)
In C++ operator overloading is used and it's OK too.

Is there a nice way to do the above in D?
As far as I know, D operator overloading works only with objects and structs.
I'm not a fan of the own-string-class philosophy, so I'm actually talking about
char[].

This question is not about printf and friends.

I love the way it works in java, It's just perfect for SMALL things like the example above.

What's your opinion?



April 28, 2005
String concatenation is operator '~'

func("unexpected symbol: " ~ c);

but the best way is to use implicit format
func( format( "unexpected symbol: %d",i) );


"B.G." <B.G._member@pathlink.com> wrote in message news:d4r3hk$16nq$1@digitaldaemon.com...
> Hi!
>
> I was trying to port a C++ project to D.
> I have many inplace string manipulations like this:
>
> func("unexpected symbol: " + c);
>
> where c may be any base type like char, int, etc.
>
> In java those operations are part of the language
> (this solution works perfectly with java language as such)
> In C++ operator overloading is used and it's OK too.
>
> Is there a nice way to do the above in D?
> As far as I know, D operator overloading works only with objects and
> structs.
> I'm not a fan of the own-string-class philosophy, so I'm actually talking
> about
> char[].
>
> This question is not about printf and friends.
>
> I love the way it works in java, It's just perfect for SMALL things like
> the
> example above.
>
> What's your opinion?
>
>
> 


April 28, 2005
In article <d4r6cu$1a7c$1@digitaldaemon.com>, Andrew Fedoniouk says...
>
>String concatenation is operator '~'
>
>func("unexpected symbol: " ~ c);

but ~ only works if both operands are arrays, the above doesn't even work if c is a char. (at least with gdc compiler)

>but the best way is to use implicit format
>func( format( "unexpected symbol: %d",i) );
>
>"B.G." <B.G._member@pathlink.com> wrote in message news:d4r3hk$16nq$1@digitaldaemon.com...
>> Hi!
>>
>> I was trying to port a C++ project to D.
>> I have many inplace string manipulations like this:
>>
>> func("unexpected symbol: " + c);
>>
>> where c may be any base type like char, int, etc.
>>
>> In java those operations are part of the language
>> (this solution works perfectly with java language as such)
>> In C++ operator overloading is used and it's OK too.
>>
>> Is there a nice way to do the above in D?
>> As far as I know, D operator overloading works only with objects and
>> structs.
>> I'm not a fan of the own-string-class philosophy, so I'm actually talking
>> about
>> char[].
>>
>> This question is not about printf and friends.
>>
>> I love the way it works in java, It's just perfect for SMALL things like
>> the
>> example above.
>>
>> What's your opinion?
>>
>>
>> 
>
>


April 28, 2005
Well, you could use format as above, or a temporary array:

char[] arr = "unexpected symbol: ";
arr ~= c;
func(arr);

Which isn't beautiful, yes.  If we had array literals, which I would love but we do not, that would most likely be the simplest way.

-[Unknown]


> In article <d4r6cu$1a7c$1@digitaldaemon.com>, Andrew Fedoniouk says...
> 
>>String concatenation is operator '~'
>>
>>func("unexpected symbol: " ~ c);
> 
> 
> but ~ only works if both operands are arrays, the above doesn't even work
> if c is a char. (at least with gdc compiler)
> 
> 
>>but the best way is to use implicit format
>>func( format( "unexpected symbol: %d",i) );
April 28, 2005
On Thu, 28 Apr 2005 16:38:12 +0000 (UTC), B.G. wrote:

> Hi!
> 
> I was trying to port a C++ project to D.
> I have many inplace string manipulations like this:
> 
> func("unexpected symbol: " + c);
> 
> where c may be any base type like char, int, etc.
> 
> In java those operations are part of the language
> (this solution works perfectly with java language as such)
> In C++ operator overloading is used and it's OK too.
> 
> Is there a nice way to do the above in D?
> As far as I know, D operator overloading works only with objects and structs.
> I'm not a fan of the own-string-class philosophy, so I'm actually talking about
> char[].
Yes, it is unfortunate that string concatenation cannot be done as simply as you would like. That is mainly because you can't provide operator overrides to arrays. However, to get the effect you want is not a great effort to do in D. Here some example code that I hope helps ..

<code>
private import std.stdio;
private import std.string;

// Template for arbitrary native data type string concatenation
template func(T)
{
    char[] func(char[] pData, T pExtra)
    {
        // convert input to string, concatenate and return result
        return std.string.format("%s%s", pData, pExtra);
    }
}

// Make a few aliases to improve readability.
alias func!(char)   funcmsg;
alias func!(int)    funcmsg;
alias func!(double) funcmsg;

void main()
{
    char lChar;
    int lInt;
    double lDouble;

    lChar = 'a';
    lInt = 3;
    lDouble = 2.41;

//    func("unexpected symbol: " ~ lChar);   // fails
//    func("unexpected symbol: " ~ lInt);    // fails
//    func("unexpected symbol: " ~ lDouble); //fails

    writefln( funcmsg("unexpected symbol: " , lChar) );
    writefln( funcmsg("unexpected symbol: " , lInt) );
    writefln( funcmsg("unexpected symbol: " , lDouble) );

}
</code>

-- 
Derek Parnell
Melbourne, Australia
29/04/2005 7:07:02 AM