www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 3652] New: Allow implicit casting of dynamic array slices of known size to static array

reply d-bugmail puremagic.com writes:
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: nobody puremagic.com
        ReportedBy: baryluk smp.if.uj.edu.pl


--- Comment #0 from Witold Baryluk <baryluk 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: -------
Dec 26 2009
next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=3652



--- Comment #1 from Witold Baryluk <baryluk smp.if.uj.edu.pl> 2009-12-26
14:18:34 PST ---
So essentially problem is that slices are just dynamic arrays. So bringing back
the topic of making slices different/distinctive types.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Dec 26 2009
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=3652



--- Comment #2 from Witold Baryluk <baryluk smp.if.uj.edu.pl> 2009-12-26
14:21:40 PST ---
It is even worse that i was innitially thinking:

explicit casting doesn't work:
series2.d(113): Error: e2ir: cannot cast qs[0u..1u] of type float[] to type
float[1u]

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Dec 26 2009
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=3652


Kenji Hara <k.hara.pg gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Keywords|                            |pull


--- Comment #3 from Kenji Hara <k.hara.pg gmail.com> 2013-03-01 02:30:36 PST ---
Partial implementation:
https://github.com/D-Programming-Language/dmd/pull/1705

(In reply to comment #0)
     float[] xs = read_floats_from_file();
     foreach (i; 0 .. xs.length-4) {
          foo(xs[i .. i+4]);
     }
In this case, detecting length == 4 in compile time is difficult with complicated cases. Consider: foo(xs[(i*4+10)/2 .. (i*8>>1)/2+9]); // lwr = (i*4 + 10)/2 = i*4/2 + 10/2 = (i*2+5) // upr = (i*8>>1)/2 + 5 = (i*4/2) + 5 = i*2 + 9 = (i*2+5) + 4 So this is not supported in my pull request. -- Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email ------- You are receiving this mail because: -------
Mar 01 2013
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=3652



--- Comment #4 from github-bugzilla puremagic.com 2013-03-02 11:12:56 PST ---
Commits pushed to master at https://github.com/D-Programming-Language/dmd

https://github.com/D-Programming-Language/dmd/commit/82b79c071c9ca13785e16c9faea901fa2c45531e
partial fix Issue 3652 - Allow explicit and implicit casting of dynamic array
slices of known size to static array

If and only if both side of slice boundaries can be constant-folded, the
conversion will succeed.

Limitation:
- Does not run CTFE.
- There is no deformation of formula.

https://github.com/D-Programming-Language/dmd/commit/e74670edbf1e013da748342a83909e2bce65d993
Merge pull request #1705 from 9rnsr/ct_slice

partial fix Issue 3652 - Allow explicit and implicit casting of dynamic array
slices of known size to static array

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Mar 02 2013
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=3652


bearophile_hugs eml.cc changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |bearophile_hugs eml.cc


--- Comment #5 from bearophile_hugs eml.cc 2013-03-02 12:00:57 PST ---
(In reply to comment #3)

 So this is not supported in my pull request.
Your pull request is not perfect, but it's a significant improvement. Maybe related: Issue 8277 and Issue 9165 -- Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email ------- You are receiving this mail because: -------
Mar 02 2013
prev sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=3652



--- Comment #6 from yebblies <yebblies gmail.com> 2013-07-28 16:37:52 EST ---
*** Issue 8843 has been marked as a duplicate of this issue. ***

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