Class qualifier vs struct qualifier
Steven Schveighoffer
schveiguy at yahoo.com
Wed Jun 13 16:27:00 UTC 2018
On 6/13/18 3:35 AM, RazvanN wrote:
> Hello,
>
> I'm having a hard time understanding whether this inconsistency is a bug
> or intended behavior:
>
> immutable class Foo {}
> immutable struct Bar {}
>
> void main()
> {
> import std.stdio : writeln;
> Foo a;
> Bar b;
>
> writeln("typeof(a): ", typeof(a).stringof);
> writeln("typeof(b): ", typeof(b).stringof);
> }
>
> prints:
>
> typeof(Foo): Foo
> typeof(Bar): immutable(Bar)
>
>
> It seems like the class storage class is not taken into account which
> leads to some awkward situations like:
>
> immutable class Foo
> {
> this() {}
> }
>
> void main()
> {
> Foo a = new Foo(); // error: immutable method `this` is not
> callable using a
> // mutable object
> }
>
> To make it work I have to add immutable to both sides of the expression
> : immutable Foo a = new immutable Foo(); this is a wonder of redundancy.
> I already declared the class as immutable so it shouldn't be possible to
> have mutable instances of it (and it isn't), however I am forced to
> write the immutable twice even though it is pretty obvious that the
> class cannot be mutated.
Just on the principle of least surprise, I'd call this a bug. I don't
know what the intention is, but if the intention is for this behavior,
we should re-visit.
-Steve
More information about the Digitalmars-d-learn
mailing list