Thread overview
[Issue 799] New: Array literal with covariant types
Jan 06, 2007
d-bugmail
Jan 06, 2007
d-bugmail
Feb 03, 2007
d-bugmail
January 06, 2007
http://d.puremagic.com/issues/show_bug.cgi?id=799

           Summary: Array literal with covariant types
           Product: D
           Version: 1.00
          Platform: Other
        OS/Version: Linux
            Status: NEW
          Severity: normal
          Priority: P2
         Component: DMD
        AssignedTo: bugzilla@digitalmars.com
        ReportedBy: aarti@interia.pl


class BaseClass {}
class Class1 : BaseClass {}
class Class2 : BaseClass {}

BaseClass[] array;

void main() {
    array=[new BaseClass, new Class1, new Class2];
    //Below gives compile time error
    //Error: cannot implicitly convert expression (new Class2) of type
    //array_literal_with_covariants.Class2 to
    //array_literal_with_covariants.Class1
    array=[new Class1, new Class2];
}


-- 

January 06, 2007
http://d.puremagic.com/issues/show_bug.cgi?id=799


fvbommel@wxs.nl changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Severity|normal                      |enhancement
           Keywords|                            |spec
         OS/Version|Linux                       |All
           Platform|Other                       |All




------- Comment #1 from fvbommel@wxs.nl  2007-01-06 10:43 -------
Array literals have type T[], where T is the type of the first element (except
when that's a static array, then it's the dynamic version IIRC).
Try this:

array=[cast(BaseClass)new Class1, new Class2];

That should work.

It'd be nice if the original code worked though, so I'm changing this to an enhancement request.


-- 

February 03, 2007
http://d.puremagic.com/issues/show_bug.cgi?id=799


bugzilla@digitalmars.com changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |RESOLVED
         Resolution|                            |WONTFIX




------- Comment #2 from bugzilla@digitalmars.com  2007-02-03 01:05 -------
The request is to find the "greatest common denominator" to use as the type for an array literal. While this is possible to implement, I feel it is much more understandable to just take the type of the first element.


--