[dmd-beta] dmd 2.064 beta take 2

Don Clugston dclugston at gmail.com
Fri May 17 02:02:10 PDT 2013


> If const variable behaves like this, it is no different from
"enum" and you have two ways to do the same thing without a single way
to do what I am expecting from const variable.

No!!!!! You've got a fundamental misunderstanding here. Both those
statements are wrong.

A const member in a struct, that does not have an initializer, has ALWAYS
been stored in the struct (even in D1).
The issue is what to do with const members that have initializers.
Historically, they've been treated as manifest constants. There's a good
argument that that is wrong.
But treating them as a value stored in every struct, where every value is
the same, is even worse.
I think we just have to make it illegal to provide an initializer on a
const member.

> And what if you want to have BOTH default CTFE initialization and
additional initialization based on some run-time data?

Then you have a flaw in your thinking. That doesn't make sense.
And, I still do not believe that there is a single valid use case for the
new behaviour.

Here's the reason:
If you can provide an initializer for a struct member, that is the same for
all instances of that struct, and you will never change that member, it
shouldn't be a member of the struct in the first place. It should be static
const or an enum value.
If however you're going to change the member in the constructor, based on a
runtime value, the initializer is meaningless and shouldn't have existed.


> What do you find unacceptable/inconvenient in "-transition=XXX" approach?

Because 100% of existing cases must be changed.

This isn't even like static variables becoming shared by default, where
some of the cases needed to change to __gshared but the rest were OK with
becoming shared.
Every existing usage becomes a bug. There are no exceptions.



On 17 May 2013 10:29, Михаил Страшун <m.strashun at gmail.com> wrote:

> On Fri, May 17, 2013 at 10:54 AM, Don Clugston <dclugston at gmail.com>
> wrote:
> > You're talking about a struct static initializer. That is not the same
> as a
> > constructor.
>
> Yes, I have already been corrected and sorry for choosing wrong term.
> What essential though is that both constructor and struct literal
> initializer allow to initialize immutable struct members.
>
> > You cannot change the value of a local-scope const variable, once it has
> > been initialized.
>
> Because there is no constructor-like entity for local scope.
>
> >
> > You cannot change the value of a module-scope const variable, once it has
> > been initialized. Not even in a constructor.
>
> If this was not changed together with struct/class changes, it sounds
> like yet another bug.
>
> > Up to now:
> > You cannot change the value of a struct member const variable, once it
> has
> > been initialized. Not even in a constructor.
> >
> > I think the consistency argument acts AGAINST this new behaviour.
>
> Consistency is not about behavior of single feature (const initialized
> variable), it is more about what place it takes in the overall type
> system. If const variable behaves like this, it is no different from
> "enum" and you have two ways to do the same thing without a single way
> to do what I am expecting from const variable.
>
> >> * They have both compile-time initialization and run-time one. First
> >> is T.init, second is T(...). Needing both is a perfectly valid need
> >> from the programmer, especially with generic code in question.
> >
> >
> > I think that the reason that having both an initializer and a constructor
> > appears desirable, is actually as a workaround for the lack of struct
> > default constructors.
> > If we really need a solution for that, we should consider doing something
> > like allowing struct default constructors, but requiring them to be
> > CTFEable.
>
> And what if you want to have BOTH default CTFE initialization and
> additional initialization based on some run-time data?
>
> > Rather than allowing const members to be initialized twice, which is
> > confusing for both the programmer and the compiler to understand. (Both
> > compiler and programmer would need to inspect all of the code looking
> for a
> > possible constructor, in order to know if a const initializer is
> > meaningful).
>
> It is not more confusing than the fact that immutable is mutable in a
> constructor. Actually, most confusing part here is that this bug was
> not fixed 20-30 compiler release ago, it never made sense.
>
> > But in any case, the situation is that with this beta, code which used to
> > declare a manifest constant in a struct, now allocates memory in each
> > struct, and each copy of that memory contains the same value. Those two
> > behaviours have nothing in common, so *all* existing cases will need to
> be
> > modified. (basically by adding 'static' to the front of each one).
> > We can't silently break code in this way.
>
> Yes and I fully agree that this is a horrible breaking change. That is
> why I suggested to use "-transition=XXX" as a default approach to
> simplify such situations. And eternal gratitude goes to Kenji who has
> actually implemented it. I am completely on your side when it comes to
> silent breakages, only thing I argue is that old behavior was right.
>
> > Therefore:
> > We should make non-static const/immutable members with initializers into
> an
> > error in this release. Then all existing cases will have 'static'
> explicitly
> > added to them. Up to now it's been implicitly added, which is an
> unnecessary
> > feature and somewhat surprising behaviour.
>
> What do you find unacceptable/inconvenient in "-transition=XXX" approach?
> _______________________________________________
> dmd-beta mailing list
> dmd-beta at puremagic.com
> http://lists.puremagic.com/mailman/listinfo/dmd-beta
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.puremagic.com/pipermail/dmd-beta/attachments/20130517/f829d1fc/attachment.html>


More information about the dmd-beta mailing list