www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - bug with CTFE std.array.array ?

reply Timothee Cour <thelastmammoth gmail.com> writes:
--047d7b5d301a55e01004e131ffe6
Content-Type: text/plain; charset=ISO-8859-1

import std.array;

void main(){
  //enum a1=[1].array;//NG: Error: gc_malloc cannot be interpreted at
compile time
  enum a2=" ".array;//OK

  import std.string;
  //enum a3=" ".splitLines.array;//NG
  enum a4="".splitLines.array;//OK
  enum a5=" ".split.array;//OK
  //enum a6=" a ".split.array;//NG
  import std.algorithm:filter;
  enum a7=" a ".split.filter!(a=>true).array;
  auto a8=" a ".split.array;
  assert(a8==a7);
  enum a9=[1].filter!(a=>true).array;//OK
}


I don't understand why the NG above fail (with Error: gc_malloc cannot be
interpreted at compile time)

furthermore, it seems we can bypass the CT error with interleaving
filter!(a=>true) (see above), which is even weirder.

--047d7b5d301a55e01004e131ffe6
Content-Type: text/html; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable

<div>import std.array;</div><div><br></div><div>void main(){</div><div>=A0 =
//enum a1=3D[1].array;//NG: Error: gc_malloc cannot be interpreted at compi=
le time</div><div>=A0 enum a2=3D&quot; &quot;.array;//OK</div><div><br></di=
v><div>
=A0 import std.string;</div><div>=A0 //enum a3=3D&quot; &quot;.splitLines.a=
rray;//NG</div><div>=A0 enum a4=3D&quot;&quot;.splitLines.array;//OK</div><=
div>=A0 enum a5=3D&quot; &quot;.split.array;//OK</div><div>=A0 //enum a6=3D=
&quot; a &quot;.split.array;//NG</div>
<div>=A0 import std.algorithm:filter;</div><div>=A0 enum a7=3D&quot; a &quo=
t;.split.filter!(a=3D&gt;true).array;</div><div>=A0 auto a8=3D&quot; a &quo=
t;.split.array;</div><div>=A0 assert(a8=3D=3Da7);</div><div>=A0 enum a9=3D[=
1].filter!(a=3D&gt;true).array;//OK</div>
<div>}</div><div><br></div><div><br></div><div>I don&#39;t understand why t=
he NG above fail (with Error: gc_malloc cannot be interpreted at compile ti=
me)</div><div><br></div><div>furthermore, it seems we can bypass the CT err=
or with interleaving filter!(a=3D&gt;true) (see above), which is even weird=
er.</div>

--047d7b5d301a55e01004e131ffe6--
Jul 10 2013
parent "monarch_dodra" <monarchdodra gmail.com> writes:
On Thursday, 11 July 2013 at 01:06:12 UTC, Timothee Cour wrote:
 import std.array;

 void main(){
   //enum a1=[1].array;//NG: Error: gc_malloc cannot be 
 interpreted at
 compile time
   enum a2=" ".array;//OK

   import std.string;
   //enum a3=" ".splitLines.array;//NG
   enum a4="".splitLines.array;//OK
   enum a5=" ".split.array;//OK
   //enum a6=" a ".split.array;//NG
   import std.algorithm:filter;
   enum a7=" a ".split.filter!(a=>true).array;
   auto a8=" a ".split.array;
   assert(a8==a7);
   enum a9=[1].filter!(a=>true).array;//OK
 }


 I don't understand why the NG above fail (with Error: gc_malloc 
 cannot be
 interpreted at compile time)

 furthermore, it seems we can bypass the CT error with 
 interleaving
 filter!(a=>true) (see above), which is even weirder.

Funny, the same question was asked in learn not 3 day's ago. http://forum.dlang.org/thread/frmaptrpnrgnuvcdfczb forum.dlang.org And yeah, it was fixed. https://github.com/D-Programming-Language/phobos/pull/1305 To answer your question about "filter": filter doesn't have length, so instead of taking an efficient code branch in array, array simply becomes: foreach(e;range) arr ~= e; Which is more CTFE friendly than the optimized length implementation.
Jul 10 2013