www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 799] New: Array literal with covariant types

reply d-bugmail puremagic.com writes:
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];
}


-- 
Jan 06 2007
next sibling parent d-bugmail puremagic.com writes:
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





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.


-- 
Jan 06 2007
prev sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=799


bugzilla digitalmars.com changed:

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





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.


-- 
Feb 02 2007