www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Re: What is the difference between...

reply "Janice Caron" <caron serenityfirefly.com> writes:
-----Original Message-----
From: digitalmars-d-bounces puremagic.com 
[mailto:digitalmars-d-bounces puremagic.com] On Behalf Of Daniel919
Sent: 07 September 2007 12:39
To: digitalmars-d puremagic.com
Subject: Re: What is the difference between...

 (2) void f(const(int) x)


It is? Why? Why doesn't it mean x is a const int? So f(const(int)* x) means x is a pointer to const int, but f(const(int) x) does not mean x is a const int? Now I'm very, very confused. Why am I not getting this?
Sep 07 2007
parent reply Bruno Medeiros <brunodomedeiros+spam com.gmail> writes:
Janice Caron wrote:
 -----Original Message-----
 From: digitalmars-d-bounces puremagic.com 
 [mailto:digitalmars-d-bounces puremagic.com] On Behalf Of Daniel919
 Sent: 07 September 2007 12:39
 To: digitalmars-d puremagic.com
 Subject: Re: What is the difference between...
 
 (2) void f(const(int) x)


It is? Why? Why doesn't it mean x is a const int? So f(const(int)* x) means x is a pointer to const int, but f(const(int) x) does not mean x is a const int? Now I'm very, very confused. Why am I not getting this?

const(...) makes everything inside the parenthesis const. But there is one exception: If that const is part of a declaration, then the top-level value/type is not const. (the top-level value is the one that changes with assignments) So: const(int)* x; // mutable pointer to const int const(int) x; // mutable int; const(int*) x; // mutable pointer to const int const(int**) x; // mutable pointer to const pointer to const int; const(int*)* x; // mutable pointer to const pointer to const int; const(int)** x; // mutable pointer to mutable pointer to const int; The only way to apply const to the toplevel value, is to use const as a "storage class" (crappy name). So: const int* x; // const pointer to const int BTW, your posts are not getting threading right, could you check your client? -- Bruno Medeiros - MSc in CS/E student http://www.prowiki.org/wiki4d/wiki.cgi?BrunoMedeiros#D
Sep 07 2007
parent reply Sean Kelly <sean f4.ca> writes:
Bruno Medeiros wrote:
 Janice Caron wrote:
 -----Original Message-----
 From: digitalmars-d-bounces puremagic.com 
 [mailto:digitalmars-d-bounces puremagic.com] On Behalf Of Daniel919
 Sent: 07 September 2007 12:39
 To: digitalmars-d puremagic.com
 Subject: Re: What is the difference between...

 (2) void f(const(int) x)


It is? Why? Why doesn't it mean x is a const int? So f(const(int)* x) means x is a pointer to const int, but f(const(int) x) does not mean x is a const int? Now I'm very, very confused. Why am I not getting this?

const(...) makes everything inside the parenthesis const. But there is one exception: If that const is part of a declaration, then the top-level value/type is not const. (the top-level value is the one that changes with assignments) So: const(int)* x; // mutable pointer to const int const(int) x; // mutable int; const(int*) x; // mutable pointer to const int const(int**) x; // mutable pointer to const pointer to const int; const(int*)* x; // mutable pointer to const pointer to const int;

Wouldn't this be "mutable pointer to mutable pointer to const int?" Sean
Sep 07 2007
parent reply Bruno Medeiros <brunodomedeiros+spam com.gmail> writes:
Sean Kelly wrote:
 Bruno Medeiros wrote:
 Janice Caron wrote:
 -----Original Message-----
 From: digitalmars-d-bounces puremagic.com 
 [mailto:digitalmars-d-bounces puremagic.com] On Behalf Of Daniel919
 Sent: 07 September 2007 12:39
 To: digitalmars-d puremagic.com
 Subject: Re: What is the difference between...

 (2) void f(const(int) x)


It is? Why? Why doesn't it mean x is a const int? So f(const(int)* x) means x is a pointer to const int, but f(const(int) x) does not mean x is a const int? Now I'm very, very confused. Why am I not getting this?

const(...) makes everything inside the parenthesis const. But there is one exception: If that const is part of a declaration, then the top-level value/type is not const. (the top-level value is the one that changes with assignments) So: const(int)* x; // mutable pointer to const int const(int) x; // mutable int; const(int*) x; // mutable pointer to const int const(int**) x; // mutable pointer to const pointer to const int; const(int*)* x; // mutable pointer to const pointer to const int;

Wouldn't this be "mutable pointer to mutable pointer to const int?" Sean

The last one : const(int*)* x; ? Nope, it's like I said. It can be verified with this: x = null; // Ok *x = null; // Error **x = null; // Error If people were thinking that const(<X>) would *allways* only apply immutability to the referenced value of <X> (if X is a reference), then that's wrong. It only does that when const(<X>) is the top-level type. (BTW, naturally, the same is true for invariant) -- Bruno Medeiros - MSc in CS/E student http://www.prowiki.org/wiki4d/wiki.cgi?BrunoMedeiros#D
Sep 07 2007
parent reply Sean Kelly <sean f4.ca> writes:
Bruno Medeiros wrote:
 Sean Kelly wrote:
 Bruno Medeiros wrote:
 Janice Caron wrote:
 -----Original Message-----
 From: digitalmars-d-bounces puremagic.com 
 [mailto:digitalmars-d-bounces puremagic.com] On Behalf Of Daniel919
 Sent: 07 September 2007 12:39
 To: digitalmars-d puremagic.com
 Subject: Re: What is the difference between...

 (2) void f(const(int) x)


It is? Why? Why doesn't it mean x is a const int? So f(const(int)* x) means x is a pointer to const int, but f(const(int) x) does not mean x is a const int? Now I'm very, very confused. Why am I not getting this?

const(...) makes everything inside the parenthesis const. But there is one exception: If that const is part of a declaration, then the top-level value/type is not const. (the top-level value is the one that changes with assignments) So: const(int)* x; // mutable pointer to const int const(int) x; // mutable int; const(int*) x; // mutable pointer to const int const(int**) x; // mutable pointer to const pointer to const int; const(int*)* x; // mutable pointer to const pointer to const int;

Wouldn't this be "mutable pointer to mutable pointer to const int?" Sean

The last one : const(int*)* x; ? Nope, it's like I said.

So the "top-level value/type" rule you mention above applies to the entire type rather than just the bit in parenthesis? Then there is effectively no difference between "const(int**) x" and "const(int*)*" correct? So why are both syntaxes accepted? Sean
Sep 07 2007
parent reply Bruno Medeiros <brunodomedeiros+spam com.gmail> writes:
Sean Kelly wrote:
 Bruno Medeiros wrote:
 Sean Kelly wrote:
 Bruno Medeiros wrote:
 Janice Caron wrote:
 -----Original Message-----
 From: digitalmars-d-bounces puremagic.com 
 [mailto:digitalmars-d-bounces puremagic.com] On Behalf Of Daniel919
 Sent: 07 September 2007 12:39
 To: digitalmars-d puremagic.com
 Subject: Re: What is the difference between...

 (2) void f(const(int) x)


It is? Why? Why doesn't it mean x is a const int? So f(const(int)* x) means x is a pointer to const int, but f(const(int) x) does not mean x is a const int? Now I'm very, very confused. Why am I not getting this?

const(...) makes everything inside the parenthesis const. But there is one exception: If that const is part of a declaration, then the top-level value/type is not const. (the top-level value is the one that changes with assignments) So: const(int)* x; // mutable pointer to const int const(int) x; // mutable int; const(int*) x; // mutable pointer to const int const(int**) x; // mutable pointer to const pointer to const int; const(int*)* x; // mutable pointer to const pointer to const int;

Wouldn't this be "mutable pointer to mutable pointer to const int?" Sean

The last one : const(int*)* x; ? Nope, it's like I said.

So the "top-level value/type" rule you mention above applies to the entire type rather than just the bit in parenthesis?

Yes, if by "entire type" we mean the type of a declarations (and of typeofs too).
 Then there is 
 effectively no difference between "const(int**) x" and "const(int*)*" 
 correct? 

Nope.
 So why are both syntaxes accepted?
 
 
 Sean

I would *guess* it's so you have a way to declare a mutable class (reference) that refers to immutable class data. I.e.: const(Foo) foo; then 'foo = xpto' is allowed, but 'foo.x = xpto' is not. If const(<X>) were to work without that exception rule, then there would be no way to declare something like the above. For pointers you could do: const(int)* x; but for classes, there would be no syntax. But one would have to ask Walter to know for sure. Was any of this discussed at the conference? This 'head'/'tail' Robert has been mentioned seems related. Seems the term 'head' is the same as 'top-level value'. -- Bruno Medeiros - MSc in CS/E student http://www.prowiki.org/wiki4d/wiki.cgi?BrunoMedeiros#D
Sep 07 2007
parent reply Nathan Reed <nathaniel.reed gmail.com> writes:
Bruno Medeiros wrote:
 This 'head'/'tail' Robert 
 has been mentioned seems related. Seems the term 'head' is the same as 
 'top-level value'.
 

Yes. The 'final' storage class makes the head const, which as you've pointed out can't be done with the 'const' type constructor. So: const(int)* x; // mutable pointer to const int const(int*) x; // mutable pointer to const int final const(int)* x; // const pointer to const int final const(int*) x; // const pointer to const int (This is described on the lecture slides from the conference, so I'm not sure if it is implemented quite this way yet.) However, there is also still the 'const' storage class (as distinct from the type constructor)...it's not clear to me exactly how that differs. Does anyone else think this is much too confusing? I realize D const is a different animal from C++ const, but it seems to be badly in need of some simplification. Thanks, Nathan Reed
Sep 07 2007
next sibling parent Sean Kelly <sean f4.ca> writes:
Nathan Reed wrote:
 Bruno Medeiros wrote:
 This 'head'/'tail' Robert has been mentioned seems related. Seems the 
 term 'head' is the same as 'top-level value'.

Yes. The 'final' storage class makes the head const, which as you've pointed out can't be done with the 'const' type constructor. So: const(int)* x; // mutable pointer to const int const(int*) x; // mutable pointer to const int final const(int)* x; // const pointer to const int final const(int*) x; // const pointer to const int (This is described on the lecture slides from the conference, so I'm not sure if it is implemented quite this way yet.) However, there is also still the 'const' storage class (as distinct from the type constructor)...it's not clear to me exactly how that differs.

The 'const' storage class is essentially the same as 'const' works in D 1.0 now. The advantage being that the compiler can place the data in ROM or simply use them as compile-time constants and not store them at all.
 Does anyone else think this is much too confusing?  I realize D const is 
 a different animal from C++ const, but it seems to be badly in need of 
 some simplification.

I do. In fact, as much as I hate to say it, I actually find the C++ declarative form easier to visually parse. The new head/tail idea for D 2.0 is better than what we have now however, and I think we can do better still (though I don't have any suggestions yet regarding how). Sean
Sep 08 2007
prev sibling parent Bruno Medeiros <brunodomedeiros+spam com.gmail> writes:
Nathan Reed wrote:
 
 Does anyone else think this is much too confusing?  I realize D const is 
 a different animal from C++ const, but it seems to be badly in need of 
 some simplification.
 
 Thanks,
 Nathan Reed

Yes, I also think it's very confusing, and it's not the only aspect of the syntax I don't like, but what can we do... -- Bruno Medeiros - MSc in CS/E student http://www.prowiki.org/wiki4d/wiki.cgi?BrunoMedeiros#D
Sep 08 2007