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

           Summary: Allow implicit casting of dynamic array slices of
                    known size to static array
           Product: D
           Version: 2.036
          Platform: Other
        OS/Version: Linux
            Status: NEW
          Severity: enhancement
          Priority: P2
         Component: DMD
        AssignedTo: nob...@puremagic.com
        ReportedBy: bary...@smp.if.uj.edu.pl


--- Comment #0 from Witold Baryluk <bary...@smp.if.uj.edu.pl> 2009-12-26 
14:17:17 PST ---
Consider this:

void foo(float[4] x) {
   return x[0]+x[1]*x[2]-x[3];
}


void main() {
    float[] xs = read_floats_from_file();

    foreach (i; 0 .. xs.length-4) {
         foo(xs[ i .. i+4]);
    }
}

or simpler thing to calculate in compile time:

void main() {
    float[] xs = read_floats_from_file();
    foo(xs[ 0 .. 4]);
}

In both cases compiler will say:
   cannot implicitly convert expression (_adDupT((&
_D11TypeInfo_Af6__initZ),qs[0u..4u])) of type float[] to float[4u]
or something similar. It is hard to see why this is really needed. 

How about allowing (and performing) implicit cast to static array if compiler
knows (or can easly infere) size of the slice and it aggree with target type?
Of course there will be situations where we have no knowledge about it:


void main() {
    float[] xs = read_floats_from_file();
    uint a = rand() % xs.length;
    foo(xs[ 0 .. a ]);
}

But allowing implicit cast also can detect other errors:
    foreach (i; 0 .. xs.length-4) {
         foo(xs[ i .. i+3]);
    }

Compiler in such situation will complain not because we are using float[] to
float[4] conversion, but because we are trying to call float[4] function using
float[3] slice.

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

Reply via email to