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
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 reply "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
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.
 
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
prev sibling parent Bruce Adams <tortoise_74 yeah.who.co.uk> writes:
Robert Fraser Wrote:

 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();
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 :).
Sep 16 2007