www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - foreach, tupleof

reply Ellery Newcomer <ellery-newcomer utulsa.edu> writes:
Hello.

In D1, this code fails:

void foo(S)(ref S s){
   foreach(ref k; s.tupleof){
     k = 1;
   }
}

struct K{
   int g;
}

void main(){
   K k;
   foo(k);
   assert(k.g == 1);
}


test.d(5): Error: no storage class for value k

(referring to 'k = 1;')

Is this an expected error, and is there a good way to get the semantics 
to actually work?
Mar 07 2010
parent reply Jacob Carlborg <doob me.com> writes:
On 3/7/10 19:11, Ellery Newcomer wrote:
 Hello.

 In D1, this code fails:

 void foo(S)(ref S s){
 foreach(ref k; s.tupleof){
 k = 1;
 }
 }

 struct K{
 int g;
 }

 void main(){
 K k;
 foo(k);
 assert(k.g == 1);
 }


 test.d(5): Error: no storage class for value k

 (referring to 'k = 1;')

 Is this an expected error, and is there a good way to get the semantics
 to actually work?

It's a bug: http://d.puremagic.com/issues/show_bug.cgi?id=2411 You can use the following code as a workaround: foreach (i, dummy ; s.tupleof) s.tupleof[i] = 1;
Mar 07 2010
next sibling parent reply Ellery Newcomer <ellery-newcomer utulsa.edu> writes:
On 03/07/2010 12:23 PM, Jacob Carlborg wrote:
 On 3/7/10 19:11, Ellery Newcomer wrote:
 Hello.

 In D1, this code fails:

 void foo(S)(ref S s){
 foreach(ref k; s.tupleof){
 k = 1;
 }
 }

 struct K{
 int g;
 }

 void main(){
 K k;
 foo(k);
 assert(k.g == 1);
 }


 test.d(5): Error: no storage class for value k

 (referring to 'k = 1;')

 Is this an expected error, and is there a good way to get the semantics
 to actually work?

It's a bug: http://d.puremagic.com/issues/show_bug.cgi?id=2411 You can use the following code as a workaround: foreach (i, dummy ; s.tupleof) s.tupleof[i] = 1;

Totally awesome! Thanks a float.infinity!
Mar 07 2010
parent bearophile <bearophileHUGS lycos.com> writes:
Philippe Sigaud:
 assert(c =  ); // is there a literal for char.init? '' doesn't
 work.

I think it's '\xFF' but I don't know if it may change in future, so using char.init is probably better, more readable, more explicit in its purpose, and less prone to typing bugs. Bye, bearophile
Mar 08 2010
prev sibling next sibling parent Philippe Sigaud <philippe.sigaud gmail.com> writes:
--002215401c1239b7ab0481504683
Content-Type: text/plain; charset=ISO-8859-1

On Sun, Mar 7, 2010 at 19:53, Ellery Newcomer <ellery-newcomer utulsa.edu>wrote:

 On 03/07/2010 12:23 PM, Jacob Carlborg wrote:

 It's a bug: http://d.puremagic.com/issues/show_bug.cgi?id=2411
 You can use the following code as a workaround:

 foreach (i, dummy ; s.tupleof)
 s.tupleof[i] = 1;

Totally awesome! Thanks a float.infinity!

I remember spending *weeks* (well, OK, *hours*) trying to find a way to do such a mapping after trying the first foreach, and getting some recursive template to work... Only to find this static foreach trick, looking at Phobos code (for D2) some month after. It sent ripples in all my code... As a side note, it works for any tuple: those created by .tupleof, but also the variadic template parameters, the expansion of a std.typecons.Tuple (D2), those created by templates, etc. So you can do: void reinitialize(T...)(ref T ts) // Variadic template { foreach(i, Type; T) ts[i] = T[i].init; // iterating on the components of T. } int a = 1; char c = 'a'; string s = "abc"; reinitialize(a,c,s); assert(a = 0); // int.init assert(c = char.init); // is there a literal for char.init? '' doesn't work. assert(s=""); Philippe --002215401c1239b7ab0481504683 Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable <br><div class=3D"gmail_quote">On Sun, Mar 7, 2010 at 19:53, Ellery Newcome= r <span dir=3D"ltr">&lt;<a href=3D"mailto:ellery-newcomer utulsa.edu">eller= y-newcomer utulsa.edu</a>&gt;</span> wrote:<br><blockquote class=3D"gmail_q= uote" style=3D"margin: 0pt 0pt 0pt 0.8ex; border-left: 1px solid rgb(204, 2= 04, 204); padding-left: 1ex;"> <div><div></div><div class=3D"h5">On 03/07/2010 12:23 PM, Jacob Carlborg wr= ote:<br> <blockquote class=3D"gmail_quote" style=3D"margin: 0pt 0pt 0pt 0.8ex; borde= r-left: 1px solid rgb(204, 204, 204); padding-left: 1ex;"> It&#39;s a bug: <a href=3D"http://d.puremagic.com/issues/show_bug.cgi?id=3D= 2411" target=3D"_blank">http://d.puremagic.com/issues/show_bug.cgi?id=3D241= 1</a><br> You can use the following code as a workaround:<br> <br> foreach (i, dummy ; s.tupleof)<br> s.tupleof[i] =3D 1;<br> </blockquote> <br></div></div> Totally awesome! Thanks a float.infinity!<br></blockquote><div><br>I rememb= er spending *weeks* (well, OK, *hours*) trying to find a way to do such a m= apping after trying the first foreach, and getting some recursive template = to work... Only to find this static foreach trick, looking at Phobos code (= for D2) some month after. It sent ripples in all my code...<br> <br>As a side note, it works for any tuple: those created by .tupleof, but = also the variadic template parameters, the expansion of a std.typecons.Tupl= e (D2), those created by templates, etc. So you can do:<br><br>void reiniti= alize(T...)(ref T ts) // Variadic template<br> {<br>=A0 =A0 foreach(i, Type; T) ts[i] =3D T[i].init; // iterating on the c= omponents of T.<br>}<br><br>int a =3D 1; <br>char c =3D &#39;a&#39;; <br>st= ring s =3D &quot;abc&quot;;<br><br>reinitialize(a,c,s);<br><br>assert(a =3D= 0); // int.init<br> assert(c =3D=A0 char.init); // is there a literal for char.init? &#39;&#39;= doesn&#39;t work.<br>assert(s=3D&quot;&quot;);<br>=A0<br><br></div></div>P= hilippe<br><br> --002215401c1239b7ab0481504683--
Mar 08 2010
prev sibling parent Philippe Sigaud <philippe.sigaud gmail.com> writes:
--0015174bdd3a10e6690481515a82
Content-Type: text/plain; charset=ISO-8859-1

On Mon, Mar 8, 2010 at 22:11, bearophile <bearophileHUGS lycos.com> wrote:

 Philippe Sigaud:
 assert(c =  ); // is there a literal for char.init? '' doesn't
 work.

I think it's '\xFF' but I don't know if it may change in future, so using char.init is probably better, more readable, more explicit in its purpose, and less prone to typing bugs. Thanks. It's an invalid char, as NaN for floats?

--0015174bdd3a10e6690481515a82 Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable <br><br><div class=3D"gmail_quote">On Mon, Mar 8, 2010 at 22:11, bearophile= <span dir=3D"ltr">&lt;<a href=3D"mailto:bearophileHUGS lycos.com">bearophi= leHUGS lycos.com</a>&gt;</span> wrote:<br><blockquote class=3D"gmail_quote"= style=3D"margin: 0pt 0pt 0pt 0.8ex; border-left: 1px solid rgb(204, 204, 2= 04); padding-left: 1ex;"> Philippe Sigaud:<br> &gt; assert(c =3D =A0); // is there a literal for char.init? &#39;&#39; doe= sn&#39;t<br> &gt; work.<br> <br> I think it&#39;s &#39;\xFF&#39; but I don&#39;t know if it may change in fu= ture, so using char.init is probably better, more readable, more explicit i= n its purpose, and less prone to typing bugs.<br><br></blockquote><div> Thanks. It&#39;s an invalid char, as NaN for floats?<br><br>=A0<br></div></= div> --0015174bdd3a10e6690481515a82--
Mar 08 2010