Thread overview | ||||||
---|---|---|---|---|---|---|
|
December 01, 2007 [Issue 1703] New: invariant(Class) does not work as expected | ||||
---|---|---|---|---|
| ||||
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 -- |
December 01, 2007 Re: [Issue 1703] New: invariant(Class) does not work as expected | ||||
---|---|---|---|---|
| ||||
Posted in reply to d-bugmail | 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 :) )
|
December 01, 2007 [Issue 1703] invariant(Class) does not work as expected | ||||
---|---|---|---|---|
| ||||
Posted in reply to d-bugmail | http://d.puremagic.com/issues/show_bug.cgi?id=1703 ------- Comment #2 from markusle@gmail.com 2007-12-01 06:34 ------- 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 -- |
January 19, 2008 [Issue 1703] invariant(Class) does not work as expected | ||||
---|---|---|---|---|
| ||||
Posted in reply to d-bugmail | http://d.puremagic.com/issues/show_bug.cgi?id=1703 bugzilla@digitalmars.com changed: What |Removed |Added ---------------------------------------------------------------------------- Status|NEW |RESOLVED Resolution| |FIXED ------- Comment #3 from bugzilla@digitalmars.com 2008-01-19 03:01 ------- The docs have been fixed. -- |
Copyright © 1999-2021 by the D Language Foundation