When D is not nice
Bill Baxter
dnewsgroup at billbaxter.com
Sun Jul 6 14:33:10 PDT 2008
Frank Benoit wrote:
> Simen Kjaeraas schrieb:
>> Frank Benoit <keinfarbton at googlemail.com> wrote:
>>
>>> 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.
>>
>>
>>
>> interface A
>> {
>> string opCat(string rhs);
>> string opCat_r(string lhs);
>> }
>>
>> class B : A
>> {
>> string opCat(string rhs)
>> {
>> return toString() ~ rhs;
>> }
>>
>> string opCat_r(string lhs)
>> {
>> return lhs ~ toString();
>> }
>>
>> string toString()
>> {
>> return SomeString;
>> }
>> }
>>
>> This works for me.
>>
>> -- Simen
>
> My intention was not to ask how to solve it for a certain interface.
> Instead i was trying to show a flaw in the language design.
>
> Even if i would add object.Object method to /every/ interface, then
> still an interface ref is NOT implicit castable to an Object ref. And i
> would need to reimplement all those methods in every class that
> implements the interface.
That seems to be the price we pay for having interfaces that can be COM
interfaces as well. But I'm not sure why I should want to pay that
price, having never had a need to call a COM interface in my life.
--bb
More information about the Digitalmars-d
mailing list