Thread overview
[Issue 24454] Disallow initialization of non-static reference type data members by non-immutable values or rvalues
Apr 29
Bolpat
March 26
https://issues.dlang.org/show_bug.cgi?id=24454

Steven Schveighoffer <schveiguy@gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |schveiguy@gmail.com

--- Comment #1 from Steven Schveighoffer <schveiguy@gmail.com> ---
case c2 seems fine to me. What is the problem with it? This seems akin to string interning.

--
March 27
https://issues.dlang.org/show_bug.cgi?id=24454

Nick Treleaven <nick@geany.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |nick@geany.org

--- Comment #2 from Nick Treleaven <nick@geany.org> ---
>     static C static_c = new C(); // line 5
>     static const C static_const_c = static_c;

fieldrefinit.d(5): Error: variable `fieldrefinit.S.static_c` is a thread-local
class and cannot have a static initializer. Use `static this()` to initialize
instead.
fieldrefinit.d(6): Error: static variable `static_c` cannot be read at compile
time

It compiles if static_c is made const/immutable, but then c3 is fine.

The problem with c2 is issue 10376. I agree that c1 should not be allowed.

--
March 27
https://issues.dlang.org/show_bug.cgi?id=24454

--- Comment #3 from Nick Treleaven <nick@geany.org> ---
> The problem with c2 is issue 10376

Sorry, that's not what's happending for c2. I also don't understand why c2 is bad.

--
April 29
https://issues.dlang.org/show_bug.cgi?id=24454

--- Comment #4 from Bolpat <qs.il.paperinik@gmail.com> ---
(In reply to Steven Schveighoffer from comment #1)
> case c2 seems fine to me. What is the problem with it? This seems akin to string interning.

Strings barely have identity, but class objects in general have identity. One might expect every `S` default initialized instance has its own `immutable C` object referenced by `c2`, but that’s not the case! Surprising programmers simply isn’t a great idea and a workaround is easy.

--