digitalmars.D.learn - foreach, tupleof
- Ellery Newcomer <ellery-newcomer utulsa.edu> Mar 07 2010
- Jacob Carlborg <doob me.com> Mar 07 2010
- Ellery Newcomer <ellery-newcomer utulsa.edu> Mar 07 2010
- bearophile <bearophileHUGS lycos.com> Mar 08 2010
- Philippe Sigaud <philippe.sigaud gmail.com> Mar 08 2010
- Philippe Sigaud <philippe.sigaud gmail.com> Mar 08 2010
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
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
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
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
--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"><<a href=3D"mailto:ellery-newcomer utulsa.edu">eller= y-newcomer utulsa.edu</a>></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'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 'a'; <br>st= ring s =3D "abc";<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? ''= doesn't work.<br>assert(s=3D"");<br>=A0<br><br></div></div>P= hilippe<br><br> --002215401c1239b7ab0481504683--
Mar 08 2010
--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"><<a href=3D"mailto:bearophileHUGS lycos.com">bearophi= leHUGS lycos.com</a>></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> > assert(c =3D =A0); // is there a literal for char.init? '' doe= sn't<br> > work.<br> <br> I think it's '\xFF' but I don'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's an invalid char, as NaN for floats?<br><br>=A0<br></div></= div> --0015174bdd3a10e6690481515a82--
Mar 08 2010









bearophile <bearophileHUGS lycos.com> 