confusion about structs
Jonathan M Davis
jmdavisProg at gmx.com
Sun Sep 25 17:07:33 PDT 2011
On Monday, September 26, 2011 00:04:48 Andrej Mitrovic wrote:
> This should make things clearer for you:
>
> unittest
> {
> auto h1 = get(1);
> auto h2 = get(2);
>
> assert(h1 is h2); // both reference the same array
> }
>
> Field initialization is only done once (once per thread, or if a field
> is shared once on app start) and not each time you call the
> constructor.
Ah. Yes. Okay. There isn't a bug here then (at least not in the compiler). The
problem is that if you don't assign app in the constructor, it uses the exact
same value as is in App.init - there is no copying going on - so every
instance (or at least every instance on each thread) then points to exactly
the same array. That's definitely something that could be easy to miss if
you're not careful and is probably a sign that mutable arrays shouldn't
generally be in the init property of structs (unless they're empty).
- Jonathan M Davis
More information about the Digitalmars-d-learn
mailing list