digitalmars.D.learn - foreach, tupleof
- Ellery Newcomer (19/19) Mar 07 2010 Hello.
- Jacob Carlborg (5/24) Mar 07 2010 It's a bug: http://d.puremagic.com/issues/show_bug.cgi?id=2411
- Ellery Newcomer (2/34) Mar 07 2010 Totally awesome! Thanks a float.infinity!
- Philippe Sigaud (22/30) Mar 08 2010 I remember spending *weeks* (well, OK, *hours*) trying to find a way to ...
- bearophile (4/6) Mar 08 2010 I think it's '\xFF' but I don't know if it may change in future, so usin...
- Philippe Sigaud (1/8) 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:Totally awesome! Thanks a float.infinity!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 Sun, Mar 7, 2010 at 19:53, Ellery Newcomer <ellery-newcomer utulsa.edu>wrote:On 03/07/2010 12:23 PM, Jacob Carlborg wrote: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=""); PhilippeIt'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 08 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
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?
Mar 08 2010