digitalmars.D - Porting D 1.0 code to 2.0, 'const' -> 'invariant'
- Sean Kelly (15/15) Jan 17 2008 I know this issue has been discussed to death, but I'd like to once
- Robert Fraser (2/21) Jan 17 2008 Mixins.
- Sean Kelly (6/25) Jan 17 2008 That suggests I should do something like this:
- Robert Fraser (12/41) Jan 17 2008 I was half-kidding since it would probably be a terrible solution. I was...
- Sean Kelly (5/52) Jan 18 2008 Yeah, it would be pretty terrible :-) But it'd do the trick in a pinch.
- Steven Schveighoffer (9/65) Jan 18 2008 I know it's ugly, but for integral constants, you should be able to use ...
I know this issue has been discussed to death, but I'd like to once again question the rationale behind changing the meaning of the 'const' keyword between 1.0 and 2.0, given that the choice of keywords for const features in 2.0 seems completely arbitrary. In D 1.0, 'const' is essentially the same as 'invariant' in D 2.0. It's true that 'const' works just as well for the average situation in D 2.0, but what if I have a ton of constants in a D 1.0 library that I want to work the same way in D 2.0? ie. in D 1.0 the 'const' label means I can use the value without synchronization for multithreaded programming, etc. In D 2.0, this role is filled by 'invariant' and 'const' has been weakened to mean "read-only view," which is not at all the same thing. I suppose what I'm asking is how I should go about making a library maximally cross-compatible with D 1.0 and 2.0, given the changed meaning of 'const'? Sean
Jan 17 2008
Sean Kelly wrote:I know this issue has been discussed to death, but I'd like to once again question the rationale behind changing the meaning of the 'const' keyword between 1.0 and 2.0, given that the choice of keywords for const features in 2.0 seems completely arbitrary. In D 1.0, 'const' is essentially the same as 'invariant' in D 2.0. It's true that 'const' works just as well for the average situation in D 2.0, but what if I have a ton of constants in a D 1.0 library that I want to work the same way in D 2.0? ie. in D 1.0 the 'const' label means I can use the value without synchronization for multithreaded programming, etc. In D 2.0, this role is filled by 'invariant' and 'const' has been weakened to mean "read-only view," which is not at all the same thing. I suppose what I'm asking is how I should go about making a library maximally cross-compatible with D 1.0 and 2.0, given the changed meaning of 'const'? SeanMixins.
Jan 17 2008
Robert Fraser wrote:Sean Kelly wrote:That suggests I should do something like this: mixin("const") i = 5; But as far as I know, string mixins must be expressions or complete statements. How would I declare a series of const values using mixins? SeanI know this issue has been discussed to death, but I'd like to once again question the rationale behind changing the meaning of the 'const' keyword between 1.0 and 2.0, given that the choice of keywords for const features in 2.0 seems completely arbitrary. In D 1.0, 'const' is essentially the same as 'invariant' in D 2.0. It's true that 'const' works just as well for the average situation in D 2.0, but what if I have a ton of constants in a D 1.0 library that I want to work the same way in D 2.0? ie. in D 1.0 the 'const' label means I can use the value without synchronization for multithreaded programming, etc. In D 2.0, this role is filled by 'invariant' and 'const' has been weakened to mean "read-only view," which is not at all the same thing. I suppose what I'm asking is how I should go about making a library maximally cross-compatible with D 1.0 and 2.0, given the changed meaning of 'const'?Mixins.
Jan 17 2008
Sean Kelly wrote:Robert Fraser wrote:I was half-kidding since it would probably be a terrible solution. I was thinking something like: char[] constant(char[] declaration) { version(D_Version_2) return "invariant " ~ declaration ~ ";"; else return "const " ~ declaration ~ ";"; } mixin(constant("i = 5")); But I agree that the names should be changed.Sean Kelly wrote:That suggests I should do something like this: mixin("const") i = 5; But as far as I know, string mixins must be expressions or complete statements. How would I declare a series of const values using mixins? SeanI know this issue has been discussed to death, but I'd like to once again question the rationale behind changing the meaning of the 'const' keyword between 1.0 and 2.0, given that the choice of keywords for const features in 2.0 seems completely arbitrary. In D 1.0, 'const' is essentially the same as 'invariant' in D 2.0. It's true that 'const' works just as well for the average situation in D 2.0, but what if I have a ton of constants in a D 1.0 library that I want to work the same way in D 2.0? ie. in D 1.0 the 'const' label means I can use the value without synchronization for multithreaded programming, etc. In D 2.0, this role is filled by 'invariant' and 'const' has been weakened to mean "read-only view," which is not at all the same thing. I suppose what I'm asking is how I should go about making a library maximally cross-compatible with D 1.0 and 2.0, given the changed meaning of 'const'?Mixins.
Jan 17 2008
Robert Fraser wrote:Sean Kelly wrote:Yeah, it would be pretty terrible :-) But it'd do the trick in a pinch. I posted my message half out of frustration and half fishing for the cleanest way to do this in case we're stuck with things as they are. SeanRobert Fraser wrote:I was half-kidding since it would probably be a terrible solution. I was thinking something like: char[] constant(char[] declaration) { version(D_Version_2) return "invariant " ~ declaration ~ ";"; else return "const " ~ declaration ~ ";"; } mixin(constant("i = 5"));Sean Kelly wrote:That suggests I should do something like this: mixin("const") i = 5; But as far as I know, string mixins must be expressions or complete statements. How would I declare a series of const values using mixins? SeanI know this issue has been discussed to death, but I'd like to once again question the rationale behind changing the meaning of the 'const' keyword between 1.0 and 2.0, given that the choice of keywords for const features in 2.0 seems completely arbitrary. In D 1.0, 'const' is essentially the same as 'invariant' in D 2.0. It's true that 'const' works just as well for the average situation in D 2.0, but what if I have a ton of constants in a D 1.0 library that I want to work the same way in D 2.0? ie. in D 1.0 the 'const' label means I can use the value without synchronization for multithreaded programming, etc. In D 2.0, this role is filled by 'invariant' and 'const' has been weakened to mean "read-only view," which is not at all the same thing. I suppose what I'm asking is how I should go about making a library maximally cross-compatible with D 1.0 and 2.0, given the changed meaning of 'const'?Mixins.
Jan 18 2008
"Sean Kelly" wroteRobert Fraser wrote:I know it's ugly, but for integral constants, you should be able to use enum consistently: enum { i = 5, ... } -SteveSean Kelly wrote:Yeah, it would be pretty terrible :-) But it'd do the trick in a pinch. I posted my message half out of frustration and half fishing for the cleanest way to do this in case we're stuck with things as they are. SeanRobert Fraser wrote:I was half-kidding since it would probably be a terrible solution. I was thinking something like: char[] constant(char[] declaration) { version(D_Version_2) return "invariant " ~ declaration ~ ";"; else return "const " ~ declaration ~ ";"; } mixin(constant("i = 5"));Sean Kelly wrote:That suggests I should do something like this: mixin("const") i = 5; But as far as I know, string mixins must be expressions or complete statements. How would I declare a series of const values using mixins? SeanI know this issue has been discussed to death, but I'd like to once again question the rationale behind changing the meaning of the 'const' keyword between 1.0 and 2.0, given that the choice of keywords for const features in 2.0 seems completely arbitrary. In D 1.0, 'const' is essentially the same as 'invariant' in D 2.0. It's true that 'const' works just as well for the average situation in D 2.0, but what if I have a ton of constants in a D 1.0 library that I want to work the same way in D 2.0? ie. in D 1.0 the 'const' label means I can use the value without synchronization for multithreaded programming, etc. In D 2.0, this role is filled by 'invariant' and 'const' has been weakened to mean "read-only view," which is not at all the same thing. I suppose what I'm asking is how I should go about making a library maximally cross-compatible with D 1.0 and 2.0, given the changed meaning of 'const'?Mixins.
Jan 18 2008