Discussion:
Very strange compilation error
Jack Applegame via Digitalmars-d-learn
2014-10-15 19:29:25 UTC
Permalink
I don't understand why this code doesn't compile:

http://dpaste.dzfl.pl/dfd8df7f80ad
John Colvin via Digitalmars-d-learn
2014-10-15 20:13:19 UTC
Permalink
On Wednesday, 15 October 2014 at 19:29:27 UTC, Jack Applegame
Post by Jack Applegame via Digitalmars-d-learn
http://dpaste.dzfl.pl/dfd8df7f80ad
that should be immutable(ubyte)[] not immutable ubyte[] which is
equivalent to immutable(ubyte[]). You can't overwrite immutable
data, hence the error.
Nils Boßung via Digitalmars-d-learn
2014-10-15 21:28:03 UTC
Permalink
Post by John Colvin via Digitalmars-d-learn
On Wednesday, 15 October 2014 at 19:29:27 UTC, Jack Applegame
Post by Jack Applegame via Digitalmars-d-learn
http://dpaste.dzfl.pl/dfd8df7f80ad
that should be immutable(ubyte)[] not immutable ubyte[] which is
equivalent to immutable(ubyte[]). You can't overwrite immutable
data, hence the error.
Then

Foo1 foo1; foo1 = Foo1(); // compiles

shouldn't compile either.
John Colvin via Digitalmars-d-learn
2014-10-16 08:07:29 UTC
Permalink
Post by Nils Boßung via Digitalmars-d-learn
Post by John Colvin via Digitalmars-d-learn
On Wednesday, 15 October 2014 at 19:29:27 UTC, Jack Applegame
Post by Jack Applegame via Digitalmars-d-learn
http://dpaste.dzfl.pl/dfd8df7f80ad
that should be immutable(ubyte)[] not immutable ubyte[] which
is
equivalent to immutable(ubyte[]). You can't overwrite immutable
data, hence the error.
Then
Foo1 foo1; foo1 = Foo1(); // compiles
shouldn't compile either.
Good point. That's an accepts-invalid IMO.

Nonetheless:
union { string a; immutable ubyte[] b; }
is broken, it is effectively casting away immutability.
union { string a; immutable(ubyte)[] b; } is the correct approach.
Nils Boßung via Digitalmars-d-learn
2014-10-15 21:26:05 UTC
Permalink
Post by Jack Applegame via Digitalmars-d-learn
http://dpaste.dzfl.pl/dfd8df7f80ad
Immutability issues with unions ring a bell for me. This may be related to
issue 12885 [1] which is the result of dmd pull #2665 [2] which even
mentions std.date.SysTime.

Here's a reduced version of your test case:
---
struct Rebindable
{
union
{
int m;
immutable int i;
}
void opAssign(Rebindable another) {}
}

struct Foo3
{
// Remove the union or _timezone and dmd accepts it.
union
{
int m;
immutable int i;
}
Rebindable _timezone;
}

void main()
{
Foo3 foo3;
foo3 = Foo3();
}
---

[1] https://issues.dlang.org/show_bug.cgi?id=12885
[2] https://github.com/D-Programming-Language/dmd/pull/2665
Continue reading on narkive:
Loading...