Help needed: immutable struct is a type modifier, and that's wrong and broken
FeepingCreature
feepingcreature at gmail.com
Wed Apr 15 08:07:38 UTC 2020
On Friday, 13 March 2020 at 14:49:49 UTC, Jacob Carlborg wrote:
> On Friday, 13 March 2020 at 14:17:45 UTC, FeepingCreature wrote:
>
>> Okay, so you're saying there's an immutable modifier on S, but
>> it doesn't do anything because all the fields are immutable
>> too?
>
> Well, it looks like the spec is kind of correct:
>
> "A struct declaration can have a storage class of const,
> immutable or shared. It has an equivalent effect as declaring
> each member of the struct as const, immutable or shared"
>
> What happens if the struct doesn't have any members? If there
> are no members that can be immutable then the struct can't be
> immutable?
>
> I don't know.
>
> --
> /Jacob Carlborg
Late answer: the problem is that the storage class *doesn't* just
declare each member as immutable. If you declare
```
immutable struct S { int i; }
```
then this is *not* the same as
```
struct S { immutable int i; }
```
but rather something that behaves similar to
```
private struct S_ { immutable int i; }
alias S = immutable(S_);
```
because the compiler can't differentiate between `immutable
struct S` and `immutable(S)`.
Which results in the problem that `Unqual!S` yields `S_`, which
is a type that isn't supposed to exist, or at least not in a
end-user visible way.
More information about the Digitalmars-d
mailing list