www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 1703] New: invariant(Class) does not work as expected

reply d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=1703

           Summary: invariant(Class) does not work as expected
           Product: D
           Version: 2.008
          Platform: PC
        OS/Version: Linux
            Status: NEW
          Severity: normal
          Priority: P2
         Component: DMD
        AssignedTo: bugzilla digitalmars.com
        ReportedBy: markusle gmail.com


Hi,

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)

Also, am I assuming correctly that the following should also compile? 
If not, how else would one initialize the class?

class C
{
  this(int x) { x_ = x; }
  int x_;
}

void main()
{
  invariant(C) c;
  c = new C(10);      // ok ??
}


Thanks,
Markus


-- 
Nov 30 2007
next sibling parent "Janice Caron" <caron800 googlemail.com> writes:
On 12/1/07, d-bugmail puremagic.com <d-bugmail 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 :) )
Nov 30 2007
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=1703






Hi Janice,

Thanks much for your comments and casting works fine
indeed. The main reason I brought this up was that the
docs at http://www.digitalmars.com/d/const3.html
explicitly state that what I posted should be possible 
(the snippet of code is pretty much verbatim taken from there).
and works fine for structs but not for classes.

Hopefully, I didn't completely misunderstand the docs.

cheers,
Markus


-- 
Dec 01 2007
prev sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=1703


bugzilla digitalmars.com changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |RESOLVED
         Resolution|                            |FIXED





The docs have been fixed.


-- 
Jan 19 2008