digitalmars.D.learn - Initialize array of objects not work
- Andre Pany (15/15) Aug 03 2016 Hi,
- =?UTF-8?Q?Ali_=c3=87ehreli?= (32/39) Aug 03 2016 Note that those array concatenation operators are being applied to
- Andre Pany (4/12) Aug 03 2016 Thanks a lot Ali.
- Steven Schveighoffer (4/7) Aug 04 2016 is the cast necessary? I assumed the compiler would infer the common
- Andre Pany (9/17) Aug 04 2016 I just tested it. The compiler cannot infer interface Element.
- Steven Schveighoffer (4/19) Aug 04 2016 Ah, it's an interface. I thought it was a base class.
- =?UTF-8?Q?Ali_=c3=87ehreli?= (5/25) Aug 04 2016 Oh! I see now: Since there is a linear hierarchy line among classes,
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é
Aug 03 2016
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
Aug 03 2016
On Wednesday, 3 August 2016 at 18:34:02 UTC, Ali Çehreli wrote:On 08/03/2016 10:58 AM, Andre Pany wrote:Thanks a lot Ali. Kind regards André[...](linear/equals/....)[...]common[...]quadraticCoefficient(1)~linearCoefficient(2)~equals()~constant(1); [...]
Aug 03 2016
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
Aug 04 2016
On Thursday, 4 August 2016 at 13:48:46 UTC, Steven Schveighoffer wrote:On 8/3/16 2:34 PM, Ali Çehreli wrote: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é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
Aug 04 2016
On 8/4/16 11:19 AM, Andre Pany wrote:On Thursday, 4 August 2016 at 13:48:46 UTC, Steven Schveighoffer wrote:Ah, it's an interface. I thought it was a base class. This is expected, no need to open a request. -SteveOn 8/3/16 2:34 PM, Ali Çehreli wrote: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?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
Aug 04 2016
On 08/04/2016 09:06 AM, Steven Schveighoffer wrote:On 8/4/16 11:19 AM, Andre Pany wrote: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. AliOn Thursday, 4 August 2016 at 13:48:46 UTC, Steven Schveighoffer wrote:Ah, it's an interface. I thought it was a base class. This is expected, no need to open a request. -SteveOn 8/3/16 2:34 PM, Ali Çehreli wrote: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?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
Aug 04 2016