View mode: basic / threaded / horizontal-split · Log in · Help
April 18, 2008
Array of interfaces static initialization
Hello.
I've got following array:

typedef iObject[] paramList;

and following object architecture:

abstract class iObject { ... };
class associativeArray : iObject { ... };
class block : iObject { ... };
class cIrc   : block { ... };
class command : associativeArray { ... };

and have function
Run(paramList);

..

when I use (z.B.)

command c;
cIrc this;

Run([c, this]);

irc.d(278): Error: cannot implicitly convert expression (this) of type  
ghhd.irc.
cIrc to ghhd.irc.command

both command and irc could be easily upcast to iObject,
so I think that this is absolutely legal usage (bug ?), or I've badly  
understood the array initializer?

TIA

-- Yossarian
Tato zpráva byla vytvořena převratným poštovním klientem Opery:  
http://www.opera.com/mail/
April 18, 2008
Re: Array of interfaces static initialization
On 18/04/2008, Yossarian <xtauer01@stud.fit.vutbr.cz> wrote:
>  Run([c, this]);

I think it should be

   Run([cast(iObject)c,this]);
April 18, 2008
Re: Array of interfaces static initialization
"Yossarian" wrote
> Hello.
> I've got following array:
>
> typedef iObject[] paramList;
>
> and following object architecture:
>
> abstract class iObject { ... };
> class associativeArray : iObject { ... };
> class block : iObject { ... };
> class cIrc   : block { ... };
> class command : associativeArray { ... };
>
> and have function
> Run(paramList);
>
> ..
>
> when I use (z.B.)
>
> command c;
> cIrc this;
>
> Run([c, this]);
>
> irc.d(278): Error: cannot implicitly convert expression (this) of type 
> ghhd.irc.
> cIrc to ghhd.irc.command
>
> both command and irc could be easily upcast to iObject,
> so I think that this is absolutely legal usage (bug ?), or I've badly 
> understood the array initializer?
>

Not so badly :)  Basically, the expression [c, this] is ambiguous to the 
compiler.  So it makes a decision and says whatever the type of the first 
element is the type of the array.  So it types this as command[2].  Then it 
can't implicitly convert cIrc to command automatically so it pukes.

Janice's solution tells the compiler how to declare the literal (as an 
iObject[])

What you don't realize is that the D language is structured to have 
context-free parsing, which means just because [c, this] is used as an 
argument to Run, which takes iObject[], doesn't matter.  The compiler looks 
at [c, this] without context and tries to determine the type just from those 
tokens.  This makes the compiler implementation much simpler, but makes the 
compiler less 'intuitive.'

-Steve
April 18, 2008
Re: Array of interfaces static initialization
Dne Fri, 18 Apr 2008 20:26:34 +0200 Janice Caron <caron800@googlemail.com>  
napsal/-a:

> On 18/04/2008, Yossarian <xtauer01@stud.fit.vutbr.cz> wrote:
>>  Run([c, this]);
>
> I think it should be
>
>     Run([cast(iObject)c,this]);


And what if the command object has overloaded opCast() operator? The  
explicit upcasts are forbidden then?

TIA

-- Yossarian
Tato zpráva byla vytvořena převratným poštovním klientem Opery:  
http://www.opera.com/mail/
April 18, 2008
Re: Array of interfaces static initialization
On 18/04/2008, Yossarian <xtauer01@stud.fit.vutbr.cz> wrote:
>  And what if the command object has overloaded opCast() operator? The
> explicit upcasts are forbidden then?

   iObject temp = c;
   Run([temp,this]);
April 18, 2008
Re: Array of interfaces static initialization
Dne Fri, 18 Apr 2008 20:49:37 +0200 Janice Caron <caron800@googlemail.com>  
napsal/-a:

> On 18/04/2008, Yossarian <xtauer01@stud.fit.vutbr.cz> wrote:
>>  And what if the command object has overloaded opCast() operator? The
>> explicit upcasts are forbidden then?
>
>     iObject temp = c;
>     Run([temp,this]);


I know that this is working, I used it as an hack, but it is exactly that  
I dont want to . :-)
Never mind, hope I won't need to read that code again.


Thank you all.
-- 
Tato zpráva byla vytvořena převratným poštovním klientem Opery:  
http://www.opera.com/mail/
Top | Discussion index | About this forum | D home