digitalmars.D - Help - can't ADD constness
- Janice Caron (12/12) Sep 14 2007 This won't compile under D2.004
- Robert Fraser (3/19) Sep 14 2007 You're using the const storage class, which means compile-time constant....
- Janice Caron (15/17) Sep 14 2007 That's close. Turns out I actually needed:
- Janice Caron (7/7) Sep 14 2007 PS.
- charles (4/14) Sep 15 2007 Const is a god awful abomination that no language has gotten right, the
- Bruce Adams (7/11) Sep 16 2007 As a seasoned C++ programmer I and my colleagues regularly use the c++ f...
- Bruce Adams (4/26) Sep 16 2007 Doh! That explains a lot of problems I was having a while back (I basica...
This won't compile under D2.004 class A { int x; } void main() { const a = new A(); /* Error */ } The error is Error: non-constant expression cast(const A)new A Now what's going on here? Are we not allowed to turn mutables into consts now?
Sep 14 2007
Janice Caron Wrote:This won't compile under D2.004 class A { int x; } void main() { const a = new A(); /* Error */ } The error is Error: non-constant expression cast(const A)new A Now what's going on here? Are we not allowed to turn mutables into consts now?You're using the const storage class, which means compile-time constant. Try: const(A) = new A();
Sep 14 2007
On 9/14/07, Robert Fraser <fraserofthenight gmail.com> wrote:You're using the const storage class, which means compile-time constant. Try: const(A) = new A();That's close. Turns out I actually needed: final const(A) a = new A(); Without the "final", a isn't actually const, it's only tail-const. That's because D2.0 is still using the syntax we haven't got rid of yet. Even so, that is still highly counterintuitive. I would have thought "const T" equivalent to "final const(T)" in D2.0. Sheesh! The sooner we get rid of this syntax the better. I wonder if, in D2.1 or whatever it will be called const a = new A(); will compile. I hope so, because type deduction is a marvellous thing, and if we end up having to write the A on both sides of the assignment operator, that will just irritate me. (const(auto) doesn't work).
Sep 14 2007
PS. auto a = cast(const) new A(); doesn't compile eiither. Error is Error: cannot implicitly convert expression (cast(const A)new A) of type const A to test.A (test is the module name) Now that's bizarre. It won't compile because auto gets the type wrong!
Sep 14 2007
Const is a god awful abomination that no language has gotten right, the sooner its gotten rid of the better. No one I work with has ever had a situation where const actually paid off. Janice Caron wrote:PS. auto a = cast(const) new A(); doesn't compile eiither. Error is Error: cannot implicitly convert expression (cast(const A)new A) of type const A to test.A (test is the module name) Now that's bizarre. It won't compile because auto gets the type wrong!
Sep 15 2007
charles Wrote:Const is a god awful abomination that no language has gotten right, the sooner its gotten rid of the better. No one I work with has ever had a situation where const actually paid off.As a seasoned C++ programmer I and my colleagues regularly use the c++ flavour of const with no problems. It may not be the best solution in the world but it works. I always found it a reasonably intuitive bit of syntactic sugar. That's not to say that we can't do better in D. I'd recommend reading Scott Meyers effective C++ books for a start. And perhaps we should review the deliberations that went into developing the C++ const system. By the way, what languages other than C++ have some kind of const mechanism? They've got to be worth a review too. Regards, Bruce.
Sep 16 2007
Robert Fraser Wrote:Janice Caron Wrote:Doh! That explains a lot of problems I was having a while back (I basically ended up not using const at all). That really is quite sucky and counter intuitive. How on earth did that idea get off the drawing board? Someone ought to start a thread about replacing it with something better. Oh wait, they already did :).This won't compile under D2.004 class A { int x; } void main() { const a = new A(); /* Error */ } The error is Error: non-constant expression cast(const A)new A Now what's going on here? Are we not allowed to turn mutables into consts now?You're using the const storage class, which means compile-time constant. Try: const(A) = new A();
Sep 16 2007