Thread overview
Help - can't ADD constness
Sep 14, 2007
Janice Caron
Sep 14, 2007
Robert Fraser
Sep 14, 2007
Janice Caron
Sep 14, 2007
Janice Caron
Sep 15, 2007
charles
Sep 17, 2007
Bruce Adams
September 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?
September 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();
September 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).
September 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!
September 15, 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!
September 17, 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.