www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Help - can't ADD constness

reply "Janice Caron" <caron800 googlemail.com> writes:
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
next sibling parent reply Robert Fraser <fraserofthenight gmail.com> writes:
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
parent reply charles <charlie d.com> writes:
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
parent Bruce Adams <tortoise_74 yeah.who.co.uk> writes:
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
prev sibling next sibling parent "Janice Caron" <caron800 googlemail.com> writes:
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
prev sibling parent "Janice Caron" <caron800 googlemail.com> writes:
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