digitalmars.D - const a storage class or a type modifier?
- Jens Mueller (14/14) Nov 25 2010 Hi,
- Walter Bright (3/16) Nov 25 2010 const in D1 is a storage class, in D2 it's a type modifier. The web page...
- Jens Mueller (9/26) Nov 26 2010 I'm attaching an svn diff. Can you apply it? Since one doesn't need to
- Don (3/29) Nov 26 2010 I removed the section entirely for D2. It remains unaltered for D1.
- Jens Mueller (5/21) Nov 28 2010 Removing it leaves some questions open. At least the current thread
- Andrej Mitrovic (14/34) Nov 28 2010 AFAIK "in" will guarantee you can't change a variable, even when using
- Trass3r (3/8) Nov 26 2010 I also think const char* x in D2 is equal to const(char*) x while a dire...
- Jens Mueller (17/26) Nov 26 2010 Haven't thought about that.
- Trass3r (1/10) Nov 26 2010 Don't you mean foo(const(int)*, char*, const float*)?
- Jens Mueller (6/18) Nov 26 2010 Right. Found this out myself a minute ago.
- Michel Fortin (10/36) Nov 26 2010 const(float*) or const(float)* are pretty much equivalent as a function
- Jens Mueller (17/51) Nov 26 2010 That's true.
- Michel Fortin (10/17) Nov 26 2010 I don't really have a preference. Note that for function arguments, you
- Jens Mueller (9/26) Nov 26 2010 Right. Definitely too much choice. So it becomes a matter of style and I
- Trass3r (1/4) Nov 26 2010 Isn't "in" the default (even if nothing is specified)?
- Jonathan M Davis (5/11) Nov 26 2010 No. in is the same as const scope. If in were the default, all function
- Jens Mueller (13/19) Nov 27 2010 I don't know. I never read this. I don't think that's the case.
- Jonathan M Davis (6/22) Nov 28 2010 Well, in is essentially an alias for const scope, so no, you can't chang...
- Andrej Mitrovic (4/29) Nov 28 2010 I think scope needs to be documented better in the docs. I didn't even
- lurker (2/5) Nov 28 2010 One of D's strong points is that you never can know what amazing things ...
- Andrej Mitrovic (2/10) Nov 28 2010
- Simen kjaeraas (4/5) Nov 29 2010 And here I thought we only needed 'static'...
- spir (7/8) Nov 28 2010 That has been my impression in first few weeks playing with D
Hi, I'm converting a .h to a d module. I'm following the guide on http://www.digitalmars.com/d/2.0/htomodule.html It says "D has const as a storage class, not a type modifier. Hence, just drop any const used as a type modifier" I do not understand the first sentence nor do I find dropping the const right. Why should I drop a const? In TDPL const/immutable are type qualifiers. Type qualifier is a synonym for type modifier, isn't it? And I know storage classes like static/ref. In C you have the type qualifiers const, volatile, and restrict and the storage classes auto, register, static, extern. Now const in D is a storage class? Jens
Nov 25 2010
Jens Mueller wrote:I'm converting a .h to a d module. I'm following the guide on http://www.digitalmars.com/d/2.0/htomodule.html It says "D has const as a storage class, not a type modifier. Hence, just drop any const used as a type modifier" I do not understand the first sentence nor do I find dropping the const right. Why should I drop a const? In TDPL const/immutable are type qualifiers. Type qualifier is a synonym for type modifier, isn't it? And I know storage classes like static/ref. In C you have the type qualifiers const, volatile, and restrict and the storage classes auto, register, static, extern. Now const in D is a storage class?const in D1 is a storage class, in D2 it's a type modifier. The web page needs updating.
Nov 25 2010
Content-Disposition: inline Walter Bright wrote:Jens Mueller wrote:I'm attaching an svn diff. Can you apply it? Since one doesn't need to care about const then maybe one should drop the complete section. What do you think? I find this converting tutorial quite useful. Is there anything else missing or wrong? I'd like to adjust it because it helps people to add C libraries to D. JensI'm converting a .h to a d module. I'm following the guide on http://www.digitalmars.com/d/2.0/htomodule.html It says "D has const as a storage class, not a type modifier. Hence, just drop any const used as a type modifier" I do not understand the first sentence nor do I find dropping the const right. Why should I drop a const? In TDPL const/immutable are type qualifiers. Type qualifier is a synonym for type modifier, isn't it? And I know storage classes like static/ref. In C you have the type qualifiers const, volatile, and restrict and the storage classes auto, register, static, extern. Now const in D is a storage class?const in D1 is a storage class, in D2 it's a type modifier. The web page needs updating.
Nov 26 2010
Jens Mueller wrote:Walter Bright wrote:I removed the section entirely for D2. It remains unaltered for D1. http://www.dsource.org/projects/phobos/changeset/2201Jens Mueller wrote:I'm attaching an svn diff. Can you apply it? Since one doesn't need to care about const then maybe one should drop the complete section. What do you think? I find this converting tutorial quite useful. Is there anything else missing or wrong? I'd like to adjust it because it helps people to add C libraries to D. JensI'm converting a .h to a d module. I'm following the guide on http://www.digitalmars.com/d/2.0/htomodule.html It says "D has const as a storage class, not a type modifier. Hence, just drop any const used as a type modifier" I do not understand the first sentence nor do I find dropping the const right. Why should I drop a const? In TDPL const/immutable are type qualifiers. Type qualifier is a synonym for type modifier, isn't it? And I know storage classes like static/ref. In C you have the type qualifiers const, volatile, and restrict and the storage classes auto, register, static, extern. Now const in D is a storage class?const in D1 is a storage class, in D2 it's a type modifier. The web page needs updating.
Nov 26 2010
Content-Disposition: inline Don wrote:Jens Mueller wrote:Removing it leaves some questions open. At least the current thread revealed some problems. What about the attached update? JensWalter Bright wrote:I removed the section entirely for D2. It remains unaltered for D1. http://www.dsource.org/projects/phobos/changeset/2201const in D1 is a storage class, in D2 it's a type modifier. The web page needs updating.I'm attaching an svn diff. Can you apply it? Since one doesn't need to care about const then maybe one should drop the complete section. What do you think? I find this converting tutorial quite useful. Is there anything else missing or wrong? I'd like to adjust it because it helps people to add C libraries to D. Jens
Nov 28 2010
AFAIK "in" will guarantee you can't change a variable, even when using arrays, e.g.: void foo(in int[] a) { int[] b; a[0] = 1; // won't compile a = b; // won't compile (I think?) } void foo(int[] a) { int[] b; a[0] = 1; // compiles, and changes are reflected in the caller a = b; // compiles, but it doesn't change "a" in the calling code } I don't have DMD on this PC so I haven't tested it. On 11/28/10, Jens Mueller <jens.k.mueller gmx.de> wrote:Don wrote:Jens Mueller wrote:Removing it leaves some questions open. At least the current thread revealed some problems. What about the attached update? JensWalter Bright wrote:I removed the section entirely for D2. It remains unaltered for D1. http://www.dsource.org/projects/phobos/changeset/2201const in D1 is a storage class, in D2 it's a type modifier. The web page needs updating.I'm attaching an svn diff. Can you apply it? Since one doesn't need to care about const then maybe one should drop the complete section. What do you think? I find this converting tutorial quite useful. Is there anything else missing or wrong? I'd like to adjust it because it helps people to add C libraries to D. Jens
Nov 28 2010
In TDPL const/immutable are type qualifiers. Type qualifier is a synonym for type modifier, isn't it? And I know storage classes like static/ref. In C you have the type qualifiers const, volatile, and restrict and the storage classes auto, register, static, extern. Now const in D is a storage class?I also think const char* x in D2 is equal to const(char*) x while a direct translation would be const(char)* x So you might use the former version to keep compatibility with D1.
Nov 26 2010
Trass3r wrote:Haven't thought about that. Right. In D2 const T* is equal to const(T*). Let's check all the possible cases: void foo(const int *non_const_ptr_to_const, char *const const_ptr_to_non_const, const float *const const_ptr_to_const); In D2 you cannot express char *const. That's due to the transitivity of const. You can only do a const char* which is a constant pointer to constant. That's why I think foo should become in D2 void foo(const int*, char *, const(float*)); What do you think? I do not know much about D1. Is const transitive in D1? I think the page http://www.digitalmars.com/d/2.0/const3.html should not talk about storage class in regard to const and immutable. JensIn TDPL const/immutable are type qualifiers. Type qualifier is a synonym for type modifier, isn't it? And I know storage classes like static/ref. In C you have the type qualifiers const, volatile, and restrict and the storage classes auto, register, static, extern. Now const in D is a storage class?I also think const char* x in D2 is equal to const(char*) x while a direct translation would be const(char)* x So you might use the former version to keep compatibility with D1.
Nov 26 2010
Let's check all the possible cases: void foo(const int *non_const_ptr_to_const, char *const const_ptr_to_non_const, const float *const const_ptr_to_const); In D2 you cannot express char *const. That's due to the transitivity of const. You can only do a const char* which is a constant pointer to constant. That's why I think foo should become in D2 void foo(const int*, char *, const(float*));Don't you mean foo(const(int)*, char*, const float*)?
Nov 26 2010
Trass3r wrote:Right. Found this out myself a minute ago. Thanks. And as you say const float* is equivalent to const(float*). You're right const float* is better than const(float*). JensLet's check all the possible cases: void foo(const int *non_const_ptr_to_const, char *const const_ptr_to_non_const, const float *const const_ptr_to_const); In D2 you cannot express char *const. That's due to the transitivity of const. You can only do a const char* which is a constant pointer to constant. That's why I think foo should become in D2 void foo(const int*, char *, const(float*));Don't you mean foo(const(int)*, char*, const float*)?
Nov 26 2010
On 2010-11-26 07:51:20 -0500, Jens Mueller <jens.k.mueller gmx.de> said:Trass3r wrote:const(float*) or const(float)* are pretty much equivalent as a function parameter type. That's because the pointer is being passed to the function by copy. So whether it is const or not only matter when you are writing the function's body (can't mutate the local variable). It doesn't change anything for the caller. -- Michel Fortin michel.fortin michelf.com http://michelf.com/Haven't thought about that. Right. In D2 const T* is equal to const(T*). Let's check all the possible cases: void foo(const int *non_const_ptr_to_const, char *const const_ptr_to_non_const, const float *const const_ptr_to_const); In D2 you cannot express char *const. That's due to the transitivity of const. You can only do a const char* which is a constant pointer to constant. That's why I think foo should become in D2 void foo(const int*, char *, const(float*)); What do you think?In TDPL const/immutable are type qualifiers. Type qualifier is a synonym for type modifier, isn't it? And I know storage classes like static/ref. In C you have the type qualifiers const, volatile, and restrict and the storage classes auto, register, static, extern. Now const in D is a storage class?I also think const char* x in D2 is equal to const(char*) x while a direct translation would be const(char)* x So you might use the former version to keep compatibility with D1.
Nov 26 2010
Michel Fortin wrote:On 2010-11-26 07:51:20 -0500, Jens Mueller <jens.k.mueller gmx.de> said:That's true. So how do you like it to be? void foo(const(int)*, char *, const(float)*); or void foo(const int*, char *, const float*); I still like the fixed original one namely void foo(const(int)*, char *, const float*); Because it's the most explicit one and one should know the difference. Even though one is not implementing the function. But the signature tells you something. If you do const float* it tells you that the implementation isn't changing its copied pointer. If you do const(float)* then it may change its copied value of the pointer. But I agree it's rather artificial because it's a copy anyway. But I like it to be explicit. In general I prefer the const() notation more than the one without round brackets when dealing with types. JensTrass3r wrote:const(float*) or const(float)* are pretty much equivalent as a function parameter type. That's because the pointer is being passed to the function by copy. So whether it is const or not only matter when you are writing the function's body (can't mutate the local variable). It doesn't change anything for the caller.Haven't thought about that. Right. In D2 const T* is equal to const(T*). Let's check all the possible cases: void foo(const int *non_const_ptr_to_const, char *const const_ptr_to_non_const, const float *const const_ptr_to_const); In D2 you cannot express char *const. That's due to the transitivity of const. You can only do a const char* which is a constant pointer to constant. That's why I think foo should become in D2 void foo(const int*, char *, const(float*)); What do you think?In TDPL const/immutable are type qualifiers. Type qualifier is a synonym for type modifier, isn't it? And I know storage classes like static/ref. In C you have the type qualifiers const, volatile, and restrict and the storage classes auto, register, static, extern. Now const in D is a storage class?I also think const char* x in D2 is equal to const(char*) x while a direct translation would be const(char)* x So you might use the former version to keep compatibility with D1.
Nov 26 2010
On 2010-11-26 09:12:53 -0500, Jens Mueller <jens.k.mueller gmx.de> said:So how do you like it to be? void foo(const(int)*, char *, const(float)*); or void foo(const int*, char *, const float*); I still like the fixed original one namely void foo(const(int)*, char *, const float*);I don't really have a preference. Note that for function arguments, you can also use 'in' to mean const: void foo(in int*, char *, in float*); My opinion is that we have too much choice. When everyone can write the same thing differently it can easily become confusing. -- Michel Fortin michel.fortin michelf.com http://michelf.com/
Nov 26 2010
Michel Fortin wrote:On 2010-11-26 09:12:53 -0500, Jens Mueller <jens.k.mueller gmx.de> said:Right. Definitely too much choice. So it becomes a matter of style and I think the documentation should be in one consistent style. For interfacing with C I prefer the const version over in. Because I consider in and out to be like one pair of shoes. And since out doesn't make sense when interfacing with C I don't like it. But for non interfacing code one should use in/out because they are more explicit than const. JensSo how do you like it to be? void foo(const(int)*, char *, const(float)*); or void foo(const int*, char *, const float*); I still like the fixed original one namely void foo(const(int)*, char *, const float*);I don't really have a preference. Note that for function arguments, you can also use 'in' to mean const: void foo(in int*, char *, in float*); My opinion is that we have too much choice. When everyone can write the same thing differently it can easily become confusing.
Nov 26 2010
I don't really have a preference. Note that for function arguments, you can also use 'in' to mean const: void foo(in int*, char *, in float*);Isn't "in" the default (even if nothing is specified)?
Nov 26 2010
On Friday 26 November 2010 10:24:02 Trass3r wrote:No. in is the same as const scope. If in were the default, all function parameters would have to be const or immutable (since you couldn't ever unmark them as const - though marking them as immutable would override const). - Jonathan M DavisI don't really have a preference. Note that for function arguments, you can also use 'in' to mean const: void foo(in int*, char *, in float*);Isn't "in" the default (even if nothing is specified)?
Nov 26 2010
Trass3r wrote:I don't know. I never read this. I don't think that's the case. For example void foo(in int a) { //a = 1; // won't compile } Whereas void foo(int a) { a = 1; // this compiles } compiles. So in is definitely not the default. JensI don't really have a preference. Note that for function arguments, you can also use 'in' to mean const: void foo(in int*, char *, in float*);Isn't "in" the default (even if nothing is specified)?
Nov 27 2010
On Sunday 28 November 2010 05:28:45 Andrej Mitrovic wrote:AFAIK "in" will guarantee you can't change a variable, even when using arrays, e.g.: void foo(in int[] a) { int[] b; a[0] = 1; // won't compile a = b; // won't compile (I think?) } void foo(int[] a) { int[] b; a[0] = 1; // compiles, and changes are reflected in the caller a = b; // compiles, but it doesn't change "a" in the calling code } I don't have DMD on this PC so I haven't tested it.Well, in is essentially an alias for const scope, so no, you can't change any variables marked in. And since const is transitive, that includes anything that the variable references. So, arrays marked with in are fully const and cannot be altered in any way. - Jonathan M Davis
Nov 28 2010
I think scope needs to be documented better in the docs. I didn't even know it could be used in the parameter list, or what it does for that matter. On 11/28/10, Jonathan M Davis <jmdavisProg gmx.com> wrote:On Sunday 28 November 2010 05:28:45 Andrej Mitrovic wrote:AFAIK "in" will guarantee you can't change a variable, even when using arrays, e.g.: void foo(in int[] a) { int[] b; a[0] = 1; // won't compile a = b; // won't compile (I think?) } void foo(int[] a) { int[] b; a[0] = 1; // compiles, and changes are reflected in the caller a = b; // compiles, but it doesn't change "a" in the calling code } I don't have DMD on this PC so I haven't tested it.Well, in is essentially an alias for const scope, so no, you can't change any variables marked in. And since const is transitive, that includes anything that the variable references. So, arrays marked with in are fully const and cannot be altered in any way. - Jonathan M Davis
Nov 28 2010
Andrej Mitrovic Wrote:I think scope needs to be documented better in the docs. I didn't even know it could be used in the parameter list, or what it does for that matter.One of D's strong points is that you never can know what amazing things it can do. Bearophile always complains that D doesn't do 'Foo' or 'Bar', but these undocumented features since year 200x always bring a smile to my lips. You can say 'haha' in the voice of that simpsons wiggum son character.
Nov 28 2010
D's slogan should be "There's a keyword for that." :p On 11/28/10, lurker <lurk lurking.net> wrote:Andrej Mitrovic Wrote:I think scope needs to be documented better in the docs. I didn't even know it could be used in the parameter list, or what it does for that matter.One of D's strong points is that you never can know what amazing things it can do. Bearophile always complains that D doesn't do 'Foo' or 'Bar', but these undocumented features since year 200x always bring a smile to my lips. You can say 'haha' in the voice of that simpsons wiggum son character.
Nov 28 2010
Andrej Mitrovic <andrej.mitrovich gmail.com> wrote:D's slogan should be "There's a keyword for that." :pAnd here I thought we only needed 'static'... -- Simen
Nov 29 2010
On Sun, 28 Nov 2010 15:13:23 +0000 Andrej Mitrovic <andrej.mitrovich gmail.com> wrote:D's slogan should be "There's a keyword for that." :pThat has been my impression in first few weeks playing with D Denis -- -- -- -- -- -- -- vit esse estrany =E2=98=A3 spir.wikidot.com
Nov 28 2010