Jump to page: 1 24  
Page
Thread overview
When D is not nice
Jul 06, 2008
Frank Benoit
Jul 06, 2008
Simen Kjaeraas
Jul 06, 2008
Simen Kjaeraas
Jul 06, 2008
Frank Benoit
Jul 06, 2008
Bill Baxter
Jul 06, 2008
Frank Benoit
Jul 06, 2008
bearophile
Jul 06, 2008
Frank Benoit
Jul 06, 2008
Ary Borenszweig
Jul 06, 2008
superdan
Jul 06, 2008
downs
Jul 06, 2008
Ary Borenszweig
Jul 07, 2008
Ary Borenszweig
Jul 27, 2008
Bruno Medeiros
Jul 06, 2008
superdan
Jul 07, 2008
Koroskin Denis
Jul 07, 2008
superdan
Jul 08, 2008
Nick Sabalausky
Jul 07, 2008
Leandro Lucarella
Jul 07, 2008
Leandro Lucarella
Jul 06, 2008
superdan
Jul 07, 2008
Manfred_Nowak
Jul 07, 2008
Frank Benoit
Jul 07, 2008
Manfred_Nowak
Jul 07, 2008
David Wilson
Jul 08, 2008
JAnderson
Jul 10, 2008
JAnderson
July 06, 2008
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.
July 06, 2008
Frank Benoit <keinfarbton@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
July 06, 2008
Simen Kjaeraas <simen.kjaras@gmail.com> wrote:

> This works for me.

But after thinking through it once more, I guess changing the interface definition is not really option, is it?

-- Simen
July 06, 2008
Simen Kjaeraas schrieb:
> Frank Benoit <keinfarbton@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.


July 06, 2008
Frank Benoit wrote:
> Simen Kjaeraas schrieb:
>> Frank Benoit <keinfarbton@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
July 06, 2008
Bill Baxter schrieb:
> 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

The COM is a /very/ special case, that could be handle with a special solution.
The compiler could know about IUnkown and that it is not compatible to Object.
That would make D feel much smoother, when dealing a lot with interfaces.
July 06, 2008
Frank Benoit:
> Instead i was trying to show a flaw in the language design.

I think automatic casting of types is a source of troubles. Some times I'd like to remove from D some automatic integer/unsigned casts that D inherits from C.

Bye,
bearophile
July 06, 2008
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.
July 06, 2008
bearophile schrieb:
> Frank Benoit:
>> Instead i was trying to show a flaw in the language design.
> 
> I think automatic casting of types is a source of troubles. Some times I'd like to remove from D some automatic integer/unsigned casts that D inherits from C.
> 
> Bye,
> bearophile

I think class/interface compatibility is not going to introduce trouble.
With the exception of COM, every interface IS an Object.

The missing compatibility makes the basic object functionality missing. That makes trouble with .toString as shown, with container that want to use opCmp/opEquals or functions that just want an Object of any type:

void store(Object o);

but i need a cast for interface:
store( cast(Object)a );

Yuck!


July 06, 2008
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");

« First   ‹ Prev
1 2 3 4