[Issue 1703] New: invariant(Class) does not work as expected

Janice Caron caron800 at googlemail.com
Fri Nov 30 22:23:27 PST 2007


On 12/1/07, d-bugmail at puremagic.com <d-bugmail at puremagic.com> wrote:
> According to the docs I would have expected the following to
> compile using dmd-2.008
>
> class C
> {
>     int x;
> }
>
> void main()
> {
>   invariant(C) c;
>   c = new C;      // (1) ok
> }
>
> However, dmd tells me that
>
> test.d(9): Error: cannot implicitly convert expression (new C) of type test.C
> to invariant(C)

I believe the compiler is correct. Mutable data will not implicitly
convert to invariant data. For that you need an explicit cast (new
always creates mutable data).

There is a template called assumeUnique in std.contracts, but I think
it only works for arrays. In any case, all it does is insert a cast,
so you could just do that manually, as in:

    c = cast(invariant(C)) new C;

(You could always ask for a language feature inew :) )


More information about the Digitalmars-d-bugs mailing list