When D is not nice

Ary Borenszweig ary at esperanto.org.ar
Sun Jul 6 15:43:15 PDT 2008


downs a écrit :
> superdan wrote:
>> Ary Borenszweig Wrote:
>>
>>> Frank Benoit a écrit :
>>>> String concatenation in Java:
>>>>
>>>> "abc " + a + " bla";
>>>>
>>>> where a is an interface ref.
>>>>
>>>> Ported to D, this look like this:
>>>>
>>>> "abc " ~ (cast(Object)a).toString ~ " bla";
>>>>
>>>> This are 3 steps more:
>>>> 1.) explicit cast to Object (interface/class compatibility!)
>>>> 2.) explicit call to toString
>>>> 3.) put additional parentheses
>>>>
>>>> I would be happy if we could remove all three of this annoying points.
>>> Exactly the same thought here. Also:
>>>
>>> int i = ...;
>>> char[] x = "You pressed button " + i;
>>>
>>> Oops... doesn't compile. To fix it:
>>>
>>> import std.string;
>>>
>>> int i = ...;
>>> char[] x = "You pressed button " + toString(i);
>>>
>>> But wait, if that piece of code is inside a method of a class, than 
>>> (WHY?) it thinks it's the toString() method, so you end up writing:
>>>
>>> import std.string;
>>>
>>> int i = ...;
>>> char[] x = "You pressed button " + std.string.toString(i);
>>>
>>> Those are the cases I find D not nice.
>> yarp i concur. phobos oughtta have a function asStr that converts everything to string and concats. then you write:
>>
>> auto x = asStr("You pressed button ", i, " with your pinky toe");
>>
> 
> This is also known as std.string.format.

But concatenating such things is so common... Instead of doing what it 
is obvious, you get an error. At least built-in types should have opCat 
and opCat_r defined like that.



More information about the Digitalmars-d mailing list