Jump to page: 1 2 3
Thread overview
[Issue 8910] New: Static arrays and dynamic arrays and std.array.join
Oct 29, 2012
Daniel Cousens
[Issue 8910] Static arrays, dynamic arrays and std.array.join
Oct 29, 2012
Daniel Cousens
Oct 29, 2012
Jonathan M Davis
Oct 29, 2012
timon.gehr@gmx.ch
Oct 29, 2012
Jonathan M Davis
Oct 30, 2012
Daniel Cousens
Oct 30, 2012
Jonathan M Davis
Oct 30, 2012
Daniel Cousens
Oct 30, 2012
Daniel Cousens
Oct 30, 2012
Daniel Cousens
Oct 30, 2012
timon.gehr@gmx.ch
Oct 30, 2012
timon.gehr@gmx.ch
Oct 30, 2012
Daniel Cousens
Oct 30, 2012
Daniel Cousens
Oct 30, 2012
timon.gehr@gmx.ch
Oct 30, 2012
Jonathan M Davis
October 29, 2012
http://d.puremagic.com/issues/show_bug.cgi?id=8910

           Summary: Static arrays and dynamic arrays and std.array.join
           Product: D
           Version: D2
          Platform: All
        OS/Version: All
            Status: NEW
          Severity: normal
          Priority: P2
         Component: DMD
        AssignedTo: nobody@puremagic.com
        ReportedBy: daniel350@bigpond.com


--- Comment #0 from Daniel Cousens <daniel350@bigpond.com> 2012-10-29 15:18:13 PDT ---
I imagine this bug is a mere matter of adjusting some templates, but it is a very common occurence anyway.

The simplest case:
char[5][2] cs;
cs.join(","); // fails

The same case, rewritten to use heap arrays.
char[][] cs = new char[][2];
cs[0] = new char[5];
cs[1] = new char[5];

cs.join(","); // works

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
October 29, 2012
http://d.puremagic.com/issues/show_bug.cgi?id=8910



--- Comment #1 from Daniel Cousens <daniel350@bigpond.com> 2012-10-29 15:22:18 PDT ---
The error is "cannot deduce template function from argument types
!()(char[2LU][], char[])

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
October 29, 2012
http://d.puremagic.com/issues/show_bug.cgi?id=8910


Jonathan M Davis <jmdavisProg@gmx.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |RESOLVED
                 CC|                            |jmdavisProg@gmx.com
         Resolution|                            |WONTFIX


--- Comment #2 from Jonathan M Davis <jmdavisProg@gmx.com> 2012-10-29 15:35:15 PDT ---
The problem is that you're using static arrays, and they're not ranges. They will work with some range-based functions if you slice them (as their slice is a dynamic array), but static arrays themselves will not work. But you need to be careful when slicing them and passing the slices to range-based functions, because you're then slicing memory which is on the stack, and if it escapes the function, then it'll be pointing at invalid memory (since the static array won't exist anymore).

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
October 29, 2012
http://d.puremagic.com/issues/show_bug.cgi?id=8910


bearophile_hugs@eml.cc changed:

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


--- Comment #3 from bearophile_hugs@eml.cc 2012-10-29 15:38:29 PDT ---
This too fails:

void main() {
    char[5][2] cs;
    cs[].join(",");
}


This is a work-around, but I agree it's too much complex and it produces too much garbage for the collector (all those dup):

import std.stdio, std.array, std.algorithm;
void main() {
    char[5][2] cs = 'x';
    char[] j = map!(r => r.dup)(cs[]).join(",");
    writeln(j);
}


So I agree join() should support fixed-size arrays of arrays.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
October 29, 2012
http://d.puremagic.com/issues/show_bug.cgi?id=8910



--- Comment #4 from bearophile_hugs@eml.cc 2012-10-29 15:40:38 PDT ---
(In reply to comment #2)
> The problem is that you're using static arrays, and they're not ranges. They will work with some range-based functions if you slice them (as their slice is a dynamic array), but static arrays themselves will not work. But you need to be careful when slicing them and passing the slices to range-based functions, because you're then slicing memory which is on the stack, and if it escapes the function, then it'll be pointing at invalid memory (since the static array won't exist anymore).

What you say doesn't explain why Phobos doesn't have a function to join a built-in fixed-sized 2D array turning it into a single dynamic array. So I think closing this issue is the wrong decision.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
October 29, 2012
http://d.puremagic.com/issues/show_bug.cgi?id=8910


timon.gehr@gmx.ch changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |timon.gehr@gmx.ch


--- Comment #5 from timon.gehr@gmx.ch 2012-10-29 15:45:12 PDT ---
This works without allocating:

import std.stdio, std.array, std.algorithm;
void main() {
    char[5][2] cs = 'x';
    char[] j = cs[].map!((char[]a)=>a).join(",");
    writeln(j);
}

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
October 29, 2012
http://d.puremagic.com/issues/show_bug.cgi?id=8910



--- Comment #6 from bearophile_hugs@eml.cc 2012-10-29 15:54:16 PDT ---
(In reply to comment #5)

>     char[] j = cs[].map!((char[]a)=>a).join(",");

Right, good idea. It also avoids all the stack copies of the rows! :-)

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
October 29, 2012
http://d.puremagic.com/issues/show_bug.cgi?id=8910



--- Comment #7 from Jonathan M Davis <jmdavisProg@gmx.com> 2012-10-29 16:10:46 PDT ---
> What you say doesn't explain why Phobos doesn't have a function to join a built-in fixed-sized 2D array turning it into a single dynamic array. So I think closing this issue is the wrong decision.

That would mean copying it. The solution is to slice it, and Timon's solution deals with the multi-dimensional issues posed by this particular case.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
October 30, 2012
http://d.puremagic.com/issues/show_bug.cgi?id=8910



--- Comment #8 from Daniel Cousens <daniel350@bigpond.com> 2012-10-29 23:36:15 PDT ---
Frankly thats a terrible alternative. Perhaps static array specialisations are in order then; assuming current implementations can't be modified to suit.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
October 30, 2012
http://d.puremagic.com/issues/show_bug.cgi?id=8910



--- Comment #9 from Jonathan M Davis <jmdavisProg@gmx.com> 2012-10-29 23:41:54 PDT ---
> Frankly thats a terrible alternative. Perhaps static array specialisations are in order then; assuming current implementations can't be modified to suit.

So, you think that copying a static array is a good idea? Because that's what on overload for a static array would do. And all that overload could do would be to slice the static array and pass it to the normal overload (because static arrays are _not_ ranges and _cannot_ be, because you can't pop any of their elements off), which would lead to the slice being completely invalid once the function returned, meaning that the result would be completely unsafe and invalid. So no, that wouldn't work.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
« First   ‹ Prev
1 2 3