[Issue 7318] Cannot concatenate arrays of super- and subtype

d-bugmail at puremagic.com d-bugmail at puremagic.com
Fri Jan 20 07:24:00 PST 2012


http://d.puremagic.com/issues/show_bug.cgi?id=7318



--- Comment #5 from timon.gehr at gmx.ch 2012-01-20 07:23:55 PST ---
(In reply to comment #4)
> (In reply to comment #2)
> > To further back up my point, this compiles, and it is not a bug:
> > 
> > void main(){
> >     auto a = [new Object()];
> >     auto b = [new Exception("")];
> >     a~=b;
> > }
> 
> It only is not a bug because of the special case of arrays.  That is, even
> though the array function takes two mutable arguments, the compiler can deduce
> that neither array will be molested (same for concatenation).
> 
> The same is not true for normal functions, you could not write the append
> function in user code that accepted a derived array type without applying
> const/inout to the second argument.

I could use a template to do the job:

ref A[] append(A,B)(ref A[] x, B[] y) if(is(B:A)) {
    foreach(e; y){
        x.length++;
        x[$-1] = e;
    }
    return x;
}

(it is more general than built-in append, but that could be fixed with a better
constraint)

> 
> I agree it's not a bug, but is an enhancement.  It would also be a good
> brainstorming session to figure out how to employ the same assumptions on
> normal functions.

Actually it is quite simple. If final means head-const, then final(T)[] is
covariant. The second argument to append is conceptually final(T)[] as are both
arguments to concat. With templates, those semantics can be emulated in user
code, therefore the language can be fixed consistently without introducing
another qualifier.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------


More information about the Digitalmars-d-bugs mailing list