Thread overview
Re: What is the difference between...
Sep 07, 2007
Janice Caron
Sep 07, 2007
Daniel Keep
Sep 07, 2007
Bruno Medeiros
September 07, 2007
-----Original Message-----
From: digitalmars-d-bounces@puremagic.com [mailto:digitalmars-d-bounces@puremagic.com] On Behalf Of Daniel Keep
Sent: 07 September 2007 12:58
To: digitalmars-d@puremagic.com
Subject: Re: What is the difference between...

> const(T) only has an effect on reference types.

O-Kaaaay.

Forgive me, but, that seems arbitrary and ... silly. If const(T) worked on all types, we'd have no need for const T, surely?

So, let's see if I've understood this right. Given

struct S { int x; } /* a non-reference type */
class C { int x; } /* a reference type */

void f(const(S) s, const(C) c)
{
    s.x = 5; /* allowed */
    c.x = 5; /* not allowed */
}

Is that really, really right? It's going to take me a long time before that seems intuitive!

September 07, 2007

Janice Caron wrote:
> -----Original Message-----
> From: digitalmars-d-bounces@puremagic.com
> [mailto:digitalmars-d-bounces@puremagic.com] On Behalf Of Daniel Keep
> Sent: 07 September 2007 12:58
> To: digitalmars-d@puremagic.com
> Subject: Re: What is the difference between...
> 
>> const(T) only has an effect on reference types.
> 
> O-Kaaaay.
> 
> Forgive me, but, that seems arbitrary and ... silly. If const(T) worked on all types, we'd have no need for const T, surely?
> 
> So, let's see if I've understood this right. Given
> 
> struct S { int x; } /* a non-reference type */
> class C { int x; } /* a reference type */
> 
> void f(const(S) s, const(C) c)
> {
>     s.x = 5; /* allowed */
>     c.x = 5; /* not allowed */
> }
> 
> Is that really, really right? It's going to take me a long time before that seems intuitive!

I get the feeling you haven't read the various articles on how const works in D 2.0...

D 2.0 adds, basically, five kinds of const.

1. "final" (storage class) -- makes a variable's bits immutable after
the relevant constructor has finished (so this() for classes, static
this() for modules) or immediately after it's been assigned to.  Does
not produce a literal constant in ROM.

2. "const" (storage class) -- makes a variable's bits immutable at
compile time, and transitively adds const(T) to the stored type.

3. "invariant" (storage class) -- makes a variable's bits immutable at
compile time, and transitively adds invariant(T) to the stored type.

4. "const(T)" (type constructor) -- turns any reference type T into a
read-only view of the referenced data; has no effect on non-reference types.

5. "invariant(T)" (type constructor) -- turns any reference type T into a *globally immutable* view of the referenced data.  This means that not only can't you make changes, no one else can, either.  Has no effect on non-reference types.

What you have to realise is that D 2.0's const system is *NOT* the same as C's, C++'s, D 1.0's or anyone else's.  Yes, it's weird when you first see it, but there are good reasons for it being the way it is.

Here's some links:

* http://www.digitalmars.com/d/const.html
* http://while-nan.blogspot.com/2007/06/you-cant-touch-this.html (note:
this was written before D 2.0 was released and doesn't contain the
storage class versions of "const" and "invariant", but should otherwise
be correct).

	-- Daniel
September 07, 2007
Daniel Keep wrote:
> 
> 
> 4. "const(T)" (type constructor) -- turns any reference type T into a
> read-only view of the referenced data; has no effect on non-reference types.
> 
> 5. "invariant(T)" (type constructor) -- turns any reference type T into
> a *globally immutable* view of the referenced data.  This means that not
> only can't you make changes, no one else can, either.  Has no effect on
> non-reference types.
> 

That's not quite the case, see my previous post.

-- 
Bruno Medeiros - MSc in CS/E student
http://www.prowiki.org/wiki4d/wiki.cgi?BrunoMedeiros#D