digitalmars.D - Help - can't ADD constness
- "Janice Caron" <caron800 googlemail.com> Sep 14 2007
- Robert Fraser <fraserofthenight gmail.com> Sep 14 2007
- charles <charlie d.com> Sep 15 2007
- Bruce Adams <tortoise_74 yeah.who.co.uk> Sep 16 2007
- "Janice Caron" <caron800 googlemail.com> Sep 14 2007
- "Janice Caron" <caron800 googlemail.com> Sep 14 2007
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
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.
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
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









Bruce Adams <tortoise_74 yeah.who.co.uk> 