immutable promise broken in unions?

2016-01-02 Thread Shriramana Sharma via Digitalmars-d-learn
import std.stdio; union EarthLocation { struct { immutable double lon, lat, alt; } double[3] data; } void main() { EarthLocation d = {data: [4, 5, 6]}; writeln(d.data); d.data = [1, 2, 3]; writeln(d.data); } I get the output: [4, 5, 6] [1, 2, 3] I thought the promise of

Re: immutable promise broken in unions?

2016-01-02 Thread Meta via Digitalmars-d-learn
On Saturday, 2 January 2016 at 12:07:31 UTC, John Colvin wrote: You are manually breaking immutable by making a union of immutable and mutable data and then writing to the mutable reference. This is roughly equivalent to casting away immutable and then writing to the reference. It's a bug in

Re: immutable promise broken in unions?

2016-01-02 Thread John Colvin via Digitalmars-d-learn
On Saturday, 2 January 2016 at 10:04:47 UTC, Shriramana Sharma wrote: import std.stdio; union EarthLocation { struct { immutable double lon, lat, alt; } double[3] data; } void main() { EarthLocation d = {data: [4, 5, 6]}; writeln(d.data); d.data = [1, 2, 3];

Re: immutable promise broken in unions?

2016-01-02 Thread John Colvin via Digitalmars-d-learn
On Saturday, 2 January 2016 at 12:08:48 UTC, Meta wrote: On Saturday, 2 January 2016 at 12:07:31 UTC, John Colvin wrote: You are manually breaking immutable by making a union of immutable and mutable data and then writing to the mutable reference. This is roughly equivalent to casting away

Re: immutable promise broken in unions?

2016-01-02 Thread Marc Schütz via Digitalmars-d-learn
On Saturday, 2 January 2016 at 12:08:48 UTC, Meta wrote: On Saturday, 2 January 2016 at 12:07:31 UTC, John Colvin wrote: You are manually breaking immutable by making a union of immutable and mutable data and then writing to the mutable reference. This is roughly equivalent to casting away

Re: immutable promise broken in unions?

2016-01-02 Thread Shriramana Sharma via Digitalmars-d-learn
John Colvin wrote: > Casting away immutable can sometimes be necessary (e.g. when > talking to other languages), so I'm not sure it should be > disallowed, but it'd be great if it was somehow easier to catch > these bugs. Yes it was in the context of talking to C that I needed to make such a

Re: immutable promise broken in unions?

2016-01-02 Thread Tobi G. via Digitalmars-d-learn
On Saturday, 2 January 2016 at 10:04:47 UTC, Shriramana Sharma wrote: I thought the promise of `immutable` was: never changes. The compiler doesn't protect you by carrying a bomb. :) But there is another usecase where it makes sense to allow writing to other union members despite the

Re: immutable promise broken in unions?

2016-01-02 Thread tsbockman via Digitalmars-d-learn
On Saturday, 2 January 2016 at 13:15:37 UTC, John Colvin wrote: Casting away immutable can sometimes be necessary (e.g. when talking to other languages), so I'm not sure it should be disallowed, but it'd be great if it was somehow easier to catch these bugs. It should be disallowed in @safe