Meaning of const

2012-01-24 Thread H. S. Teoh
s there a difference between f1 and f2? If not, which is the preferred syntax? If yes, what is the meaning of 'const' in either case? I tried various code inside f1 and f2, and it seems in both of them I can't change x, which appears to mean that 'const' in this case refers

Re: Meaning of const

2012-01-24 Thread Jonathan M Davis
.. } > const(int) f3() { ... } > } > > Is there a difference between f1 and f2? If not, which is the preferred > syntax? If yes, what is the meaning of 'const' in either case? > > I tried various code inside f1 and f2, and it seems in both of them I > can't change

Re: Meaning of const

2012-01-24 Thread Ali Çehreli
On 01/24/2012 04:06 PM, Jonathan M Davis wrote: >> class A { >> int x; >> >> const int f1() { ... } >> int f2() const { ... } >> const(int) f3() { ... } >> } >> [...] > int f2() const > > becomes > > int f2(const A this) > > Now, the confusing part is the fact that unlike C++, D allows you to

Re: Meaning of const

2012-01-24 Thread H. S. Teoh
On Tue, Jan 24, 2012 at 07:06:47PM -0500, Jonathan M Davis wrote: [...] > So, while a C++ programmer expects that > > int f2() const > > means that f2 is const, they're likely to be surprised by the fact that in > > const int f1() > > it's the int that's const, not f1. [...] Wait, I thought 'c

Re: Meaning of const

2012-01-24 Thread bearophile
Jonathan M Davis: > Now, the confusing part is the fact that unlike C++, D allows you to put the > const for making the function on the _left-hand_ side of the function (C++ > only lets you put it on the right). This is to increase consistency between > modifers (public, override, pure, etc.) -

Re: Meaning of const

2012-01-25 Thread Jonathan M Davis
On Tuesday, January 24, 2012 16:55:16 H. S. Teoh wrote: > On Tue, Jan 24, 2012 at 07:06:47PM -0500, Jonathan M Davis wrote: > [...] > > > So, while a C++ programmer expects that > > > > int f2() const > > > > means that f2 is const, they're likely to be surprised by the fact that > > in > > > >

Re: Meaning of const

2012-01-25 Thread H. S. Teoh
On Tue, Jan 24, 2012 at 08:01:41PM -0500, bearophile wrote: > Jonathan M Davis: > > > Now, the confusing part is the fact that unlike C++, D allows you to put > > the > > const for making the function on the _left-hand_ side of the function (C++ > > only lets you put it on the right). This is t

Re: Meaning of const

2012-01-25 Thread Jonathan M Davis
On Tuesday, January 24, 2012 16:24:27 Ali Çehreli wrote: > On 01/24/2012 04:06 PM, Jonathan M Davis wrote: > >> class A { > >> int x; > >> > >> const int f1() { ... } > >> int f2() const { ... } > >> const(int) f3() { ... } > >> } > > [...] > > > int f2() const > > > > becomes > > >

Re: Meaning of const

2012-01-25 Thread Timon Gehr
On 01/25/2012 02:29 AM, H. S. Teoh wrote: On Tue, Jan 24, 2012 at 08:01:41PM -0500, bearophile wrote: Jonathan M Davis: Now, the confusing part is the fact that unlike C++, D allows you to put the const for making the function on the _left-hand_ side of the function (C++ only lets you put it o

Re: Meaning of const

2012-01-25 Thread H. S. Teoh
On Wed, Jan 25, 2012 at 11:50:57PM +0100, Timon Gehr wrote: > On 01/25/2012 02:29 AM, H. S. Teoh wrote: [...] > >But since Walter doesn't like the idea of restricting the syntax to 'int > >y() const', then what about making it mandatory to write: > > > > const(int) x; > > > >instead of: > > > >

Re: Meaning of const

2012-01-25 Thread Timon Gehr
On 01/26/2012 12:35 AM, H. S. Teoh wrote: On Wed, Jan 25, 2012 at 11:50:57PM +0100, Timon Gehr wrote: On 01/25/2012 02:29 AM, H. S. Teoh wrote: [...] But since Walter doesn't like the idea of restricting the syntax to 'int y() const', then what about making it mandatory to write: cons

Meaning of const variables

2016-06-20 Thread jmh530 via Digitalmars-d-learn
I feel like I have a reasonable understanding of when to use const as a parameter in a function or for const member functions. However, I don't really understand why/when it should be used as a type modifier. For instance, the Programming in D book basically just says (http://ddili.org/ders/d

Re: Meaning of const variables

2016-06-20 Thread ketmar via Digitalmars-d-learn
On Tuesday, 21 June 2016 at 02:24:03 UTC, jmh530 wrote: So the line from the spec is "Const types are like immutable types, except that const forms a read-only view of data. Other aliases to that same data may change it at any time." I tried making an alias of a const variable and modifying i

Re: Meaning of const variables

2016-06-20 Thread H. S. Teoh via Digitalmars-d-learn
On Tue, Jun 21, 2016 at 02:24:03AM +, jmh530 via Digitalmars-d-learn wrote: > I feel like I have a reasonable understanding of when to use const as > a parameter in a function or for const member functions. However, I > don't really understand why/when it should be used as a type modifier. > >

Re: Meaning of const variables

2016-06-21 Thread Mike Parker via Digitalmars-d-learn
On Tuesday, 21 June 2016 at 02:24:03 UTC, jmh530 wrote: I tried making an alias of a const variable and modifying it, but that didn't work. So presumably they mean something else. By alias they mean another reference (or view) to the data, as in Ketmar's example and your assignment of a &x1 t

Re: Meaning of const variables

2016-06-21 Thread Mike Parker via Digitalmars-d-learn
behavior is identical. And really, if you never need to take the address of the variable, then a manifest constant using enum would be more appropriate. Actually, I should say it *may* be more appropriate. Definitely only when the initializer is known at compile time. There are cases when y

Re: Meaning of const variables

2016-06-21 Thread jmh530 via Digitalmars-d-learn
On Tuesday, 21 June 2016 at 02:54:06 UTC, ketmar wrote: Thanks to you and the others for the detailed clarifications. I think part of the difficulty is that there are basically three things being discussed that are all very similar: an alias to data, a pointer to data, and a view of data.

Re: Meaning of const variables

2016-06-21 Thread Ali Çehreli via Digitalmars-d-learn
On 06/21/2016 06:48 AM, jmh530 wrote: > So an immutable pointer guarantees that whatever it is pointing to will > never change, Yes but saying "_requires_ that data never changes" is more correct. When it comes to a pointer (i.e. the user of data), "guarantee" is related to const. > but a co

Re: Meaning of const variables

2016-06-21 Thread jmh530 via Digitalmars-d-learn
On Tuesday, 21 June 2016 at 15:21:01 UTC, Ali Çehreli wrote: [snip] Thanks.

Re: Meaning of const variables

2016-06-21 Thread H. S. Teoh via Digitalmars-d-learn
On Tue, Jun 21, 2016 at 01:48:27PM +, jmh530 via Digitalmars-d-learn wrote: [...] > Thanks to you and the others for the detailed clarifications. I think > part of the difficulty is that there are basically three things being > discussed that are all very similar: an alias to data, a pointer to