Re: Initialize array of objects not work

2016-08-04 Thread Ali Çehreli via Digitalmars-d-learn

On 08/04/2016 09:06 AM, Steven Schveighoffer wrote:

On 8/4/16 11:19 AM, Andre Pany wrote:

On Thursday, 4 August 2016 at 13:48:46 UTC, Steven Schveighoffer wrote:

On 8/3/16 2:34 PM, Ali Çehreli wrote:


void main() {
Element[] elements = cast(Element[])[ quadraticCoefficient(1),
linearCoefficient(2), equals(), constant(1) ];


is the cast necessary? I assumed the compiler would infer the common
base type...

-Steve


I just tested it. The compiler cannot infer interface Element.
By changing the type from interface to class the compiler
is happy without the cast.
Does it worth to open an enhancement request for this specific issue?


Ah, it's an interface. I thought it was a base class.

This is expected, no need to open a request.

-Steve



Oh! I see now: Since there is a linear hierarchy line among classes, 
it's possible to determine the closest common ancestor. That's not 
possible for interfaces due to multiple interface inheritance.


Ali



Re: Initialize array of objects not work

2016-08-04 Thread Steven Schveighoffer via Digitalmars-d-learn

On 8/4/16 11:19 AM, Andre Pany wrote:

On Thursday, 4 August 2016 at 13:48:46 UTC, Steven Schveighoffer wrote:

On 8/3/16 2:34 PM, Ali Çehreli wrote:


void main() {
Element[] elements = cast(Element[])[ quadraticCoefficient(1),
linearCoefficient(2), equals(), constant(1) ];


is the cast necessary? I assumed the compiler would infer the common
base type...

-Steve


I just tested it. The compiler cannot infer interface Element.
By changing the type from interface to class the compiler
is happy without the cast.
Does it worth to open an enhancement request for this specific issue?


Ah, it's an interface. I thought it was a base class.

This is expected, no need to open a request.

-Steve



Re: Initialize array of objects not work

2016-08-04 Thread Andre Pany via Digitalmars-d-learn
On Thursday, 4 August 2016 at 13:48:46 UTC, Steven Schveighoffer 
wrote:

On 8/3/16 2:34 PM, Ali Çehreli wrote:


void main() {
Element[] elements = cast(Element[])[ 
quadraticCoefficient(1),

linearCoefficient(2), equals(), constant(1) ];


is the cast necessary? I assumed the compiler would infer the 
common base type...


-Steve


I just tested it. The compiler cannot infer interface Element.
By changing the type from interface to class the compiler
is happy without the cast.
Does it worth to open an enhancement request for this specific 
issue?


Kind regards
André


Re: Initialize array of objects not work

2016-08-04 Thread Steven Schveighoffer via Digitalmars-d-learn

On 8/3/16 2:34 PM, Ali Çehreli wrote:


void main() {
Element[] elements = cast(Element[])[ quadraticCoefficient(1),
linearCoefficient(2), equals(), constant(1) ];


is the cast necessary? I assumed the compiler would infer the common 
base type...


-Steve


Re: Initialize array of objects not work

2016-08-03 Thread Andre Pany via Digitalmars-d-learn

On Wednesday, 3 August 2016 at 18:34:02 UTC, Ali Çehreli wrote:

On 08/03/2016 10:58 AM, Andre Pany wrote:

> [...]
(linear/equals/)
> [...]
common
> [...]
quadraticCoefficient(1)~linearCoefficient(2)~equals()~constant(1);

[...]


Thanks a lot Ali.

Kind regards
André


Re: Initialize array of objects not work

2016-08-03 Thread Ali Çehreli via Digitalmars-d-learn

On 08/03/2016 10:58 AM, Andre Pany wrote:

> I try to initialize an array of objects. The methods (linear/equals/)
> returns object of different classes, but all implement a common
> interface "Element".
>
> Element[] elements =
> quadraticCoefficient(1)~linearCoefficient(2)~equals()~constant(1);

Note that those array concatenation operators are being applied to 
individual object. Unless you define that operator for your types, it 
won't work.


> I tried different casts and different ways but it seems, currently D
> does not support this syntax?

Not that syntax but D is supposed to figure out the closest common 
ancestor. Unfortunately, the following code requires that cast:


interface Element {
}

class Foo : Element {
}

class Bar : Element {
}

Foo quadraticCoefficient(int) {
return new Foo();
}

Bar linearCoefficient(int) {
return new Bar();
}

Foo equals() {
return new Foo();
}

Bar constant(int) {
return new Bar();
}

void main() {
Element[] elements = cast(Element[])[ quadraticCoefficient(1), 
linearCoefficient(2), equals(), constant(1) ];

elements ~= quadraticCoefficient(1);
elements = elements ~ linearCoefficient(2);
// etc.
}

Ali



Initialize array of objects not work

2016-08-03 Thread Andre Pany via Digitalmars-d-learn

Hi,

I try to initialize an array of objects. The methods 
(linear/equals/)
returns object of different classes, but all implement a common 
interface "Element".


Element[] elements = 
quadraticCoefficient(1)~linearCoefficient(2)~equals()~constant(1);


I tried different casts and different ways but it seems, 
currently D does not support this syntax?
I always get some strange error like: Incompatible types for  
object.Object and object.Object.


Is there any nice way to initialize an array of objects without 
much syntax overhead?


Kind regards
André