digitalmars.D - 'final' variables
- Lionello Lunesu (16/16) Mar 20 2007 I've been trying to follow the thread on the const/final/invariant
- Tyler Knott (2/5) Mar 20 2007 Because the "const" keyword is being repurposed for read-only references...
-
Stewart Gordon
(4/11)
Mar 20 2007
- Sean Kelly (9/21) Mar 20 2007 Some D reference types do not have the pointer qualifier, so this C++
- Andrei Alexandrescu (See Website For Email) (4/29) Mar 20 2007 I _swear_ I hadn't seen this post when posted one with the same syntax
- Frits van Bommel (11/21) Mar 20 2007 If you apply "const" to a class reference, how would you determine which...
- Derek Parnell (12/36) Mar 20 2007 What happens with ...
- Chris Nicholson-Sauls (10/44) Mar 20 2007 In the first decleration, the referance cannot be changed, and provides ...
- Derek Parnell (10/12) Mar 20 2007 At which point I'd wish for a text macro system to make "guard" the alia...
- Lionello Lunesu (11/19) Mar 20 2007 That's my question: do we actually need that? Examples are welcomed.
- Andrei Alexandrescu (See Website For Email) (4/28) Mar 20 2007 final int x = 42;
- Benji Smith (15/24) Mar 20 2007 How does this interact with CTFE for variables that are declared as
- Andrei Alexandrescu (See Website For Email) (4/32) Mar 20 2007 Undecided at this time. At least "static final" will take care of that -...
- Benji Smith (4/39) Mar 20 2007 Damn. I always thought "static final" in Java was a sloppy kludge for
- Andrei Alexandrescu (See Website For Email) (4/6) Mar 20 2007 It is. This is because Java lacks the notion of const for
- Don Clugston (9/42) Mar 23 2007 But presumably that would allocate space for the variable in the data
- Lionello Lunesu (7/35) Mar 20 2007 const would still exist, right? So you'd just keep using "const int x =
- Lionello Lunesu (3/6) Mar 20 2007 Not fair! You meant "const" :)
- Andrei Alexandrescu (See Website For Email) (13/36) Mar 20 2007 Very simple.
- Lionello Lunesu (7/46) Mar 20 2007 I understand, but what's the point of telling the compiler "I don't want...
- Andrei Alexandrescu (See Website For Email) (4/50) Mar 20 2007 Many people, including yours truly, appreciate the ability of
- Walter Bright (19/25) Mar 20 2007 Final is handy for a number of things:
- Andrei Alexandrescu (See Website For Email) (11/32) Mar 20 2007 And the compiler will have an excellent opportunity to optimize the code...
- Derek Parnell (12/46) Mar 20 2007 Now I know (really) that new keywords are *to be avoided at all costs*, ...
- Lionello Lunesu (11/36) Mar 20 2007 Yeah, I see now how that would be handy. It's C#'s "readonly".
- James Dennett (16/23) Mar 20 2007 Same as C++ here then: the declaration
- David B. Held (28/31) Mar 21 2007 If you spend any amount of time writing Java code, you can appreciate
I've been trying to follow the thread on the const/final/invariant extensions but I have to wonder: how useful are final variables? final int i = 2; This would mean: 'i' cannot be rebound to another value. Right? So this final is actually C++'s const. void func( final int i ) { //i is not const but cannot be set either } Also here, it's like C++'s const, but with the _removed_ benefit of restricting the accepted types. Is this correct? What's the use of "final" for variables? I'm saying "for variables" because for methods the benefit is only too clear. L. PS. I've started a new thread because this isn't a reply to any particular post.
Mar 20 2007
Lionello Lunesu Wrote:What's the use of "final" for variables? I'm saying "for variables" because for methods the benefit is only too clear.Because the "const" keyword is being repurposed for read-only references to mutable or non-mutable data, we need a new keyword for non-mutable variables. "final" fills that purpose nicely. If the value of the variable is known at compile time, the compiler can constant-fold away the memory access to that variable for a small speed boost. If the value is only determinable at runtime, that can still allow the compiler to make some optimizations that would not be possible with mutable variables.
Mar 20 2007
Tyler Knott Wrote:Lionello Lunesu Wrote:<snip> C++ manages with const for both, so why can't D? What circumstance is there in which either keyword would be valid with different meanings? Stewart.What's the use of "final" for variables? I'm saying "for variables" because for methods the benefit is only too clear.Because the "const" keyword is being repurposed for read-only references to mutable or non-mutable data, we need a new keyword for non-mutable variables.
Mar 20 2007
Stewart Gordon wrote:Tyler Knott Wrote:Some D reference types do not have the pointer qualifier, so this C++ declaration: int const * const x; could not be directly reproduced in D, except for maybe: int const [] const x; which is horrifying :-) And it still doesn't address the issue of class references. SeanLionello Lunesu Wrote:<snip> C++ manages with const for both, so why can't D? What circumstance is there in which either keyword would be valid with different meanings?What's the use of "final" for variables? I'm saying "for variables" because for methods the benefit is only too clear.Because the "const" keyword is being repurposed for read-only references to mutable or non-mutable data, we need a new keyword for non-mutable variables.
Mar 20 2007
Sean Kelly wrote:Stewart Gordon wrote:I _swear_ I hadn't seen this post when posted one with the same syntax in it :o). AndreiTyler Knott Wrote:> What circumstance is there in which either keyword would be valid > with different meanings? Some D reference types do not have the pointer qualifier, so this C++ declaration: int const * const x; could not be directly reproduced in D, except for maybe: int const [] const x; which is horrifying :-) And it still doesn't address the issue of class references.Lionello Lunesu Wrote:<snip> C++ manages with const for both, so why can't D?What's the use of "final" for variables? I'm saying "for variables" because for methods the benefit is only too clear.Because the "const" keyword is being repurposed for read-only references to mutable or non-mutable data, we need a new keyword for non-mutable variables.
Mar 20 2007
Stewart Gordon wrote:Tyler Knott Wrote:If you apply "const" to a class reference, how would you determine which of {reference,object} the const applies to? - If to the reference only, you can't express the constness of objects. - If to the object only, you can't express non-rebindability of the reference. (C++ doesn't have this problem, since the only references it has are implicitly final all by themselves) - If to both, you can only express constness of objects by simultaneously disallowing the reference to be rebound. Not nice. I think that covers all cases (except 'neither', but that simply doesn't make any sense ;) ).Lionello Lunesu Wrote:<snip> C++ manages with const for both, so why can't D? What circumstance is there in which either keyword would be valid with different meanings?What's the use of "final" for variables? I'm saying "for variables" because for methods the benefit is only too clear.Because the "const" keyword is being repurposed for read-only references to mutable or non-mutable data, we need a new keyword for non-mutable variables.
Mar 20 2007
On Tue, 20 Mar 2007 16:52:14 +0100, Frits van Bommel wrote:Stewart Gordon wrote:What happens with ... final const Foo f; invariant Foo f; do either of these mean that both the reference and the object is not to be modified? -- Derek (skype: derek.j.parnell) Melbourne, Australia "Justice for David Hicks!" 21/03/2007 10:13:28 AMTyler Knott Wrote:If you apply "const" to a class reference, how would you determine which of {reference,object} the const applies to? - If to the reference only, you can't express the constness of objects. - If to the object only, you can't express non-rebindability of the reference. (C++ doesn't have this problem, since the only references it has are implicitly final all by themselves) - If to both, you can only express constness of objects by simultaneously disallowing the reference to be rebound. Not nice. I think that covers all cases (except 'neither', but that simply doesn't make any sense ;) ).Lionello Lunesu Wrote:<snip> C++ manages with const for both, so why can't D? What circumstance is there in which either keyword would be valid with different meanings?What's the use of "final" for variables? I'm saying "for variables" because for methods the benefit is only too clear.Because the "const" keyword is being repurposed for read-only references to mutable or non-mutable data, we need a new keyword for non-mutable variables.
Mar 20 2007
Derek Parnell wrote:On Tue, 20 Mar 2007 16:52:14 +0100, Frits van Bommel wrote:In the first decleration, the referance cannot be changed, and provides only an immutable view into the object, so the object cannot be modified /by/ the referance 'f'. It is a final variable, of type const Foo. In the second decleration, the referance is mutable, but the object is guaranteed unchanging. It is a standard variable, of type invariant Foo. If you wanted to absolute guarantee that 'f' itself never changes, and the underlying object does likewise, it'd be: final invariant Foo f; If I'm following all this right... -- Chris Nicholson-SaulsStewart Gordon wrote:What happens with ... final const Foo f; invariant Foo f; do either of these mean that both the reference and the object is not to be modified?Tyler Knott Wrote:If you apply "const" to a class reference, how would you determine which of {reference,object} the const applies to? - If to the reference only, you can't express the constness of objects. - If to the object only, you can't express non-rebindability of the reference. (C++ doesn't have this problem, since the only references it has are implicitly final all by themselves) - If to both, you can only express constness of objects by simultaneously disallowing the reference to be rebound. Not nice. I think that covers all cases (except 'neither', but that simply doesn't make any sense ;) ).Lionello Lunesu Wrote:<snip> C++ manages with const for both, so why can't D? What circumstance is there in which either keyword would be valid with different meanings?What's the use of "final" for variables? I'm saying "for variables" because for methods the benefit is only too clear.Because the "const" keyword is being repurposed for read-only references to mutable or non-mutable data, we need a new keyword for non-mutable variables.
Mar 20 2007
On Tue, 20 Mar 2007 18:19:29 -0600, Chris Nicholson-Sauls wrote:If you wanted to absolute guarantee that 'f' itself never changes, and the underlying object does likewise, it'd be: final invariant Foo f;At which point I'd wish for a text macro system to make "guard" the alias for "final invariant" <G> guard Foo f; -- Derek (skype: derek.j.parnell) Melbourne, Australia "Justice for David Hicks!" 21/03/2007 10:34:41 AM
Mar 20 2007
Tyler Knott wrote:Lionello Lunesu Wrote:...which I want, very much!What's the use of "final" for variables? I'm saying "for variables" because for methods the benefit is only too clear.Because the "const" keyword is being repurposed for read-only references to mutable or non-mutable data,we need a new keyword for non-mutable variables.That's my question: do we actually need that? Examples are welcomed."final" fills that purpose nicely. If the value of the variable isknown at compile time, the compiler can constant-fold away the memory access to that variable for a small speed boost. If the value is only determinable at runtime, that can still allow the compiler to make some optimizations that would not be possible with mutable variables. This is the part I'm not sure about. As with C++'s "const", I don't think the compiler can conclude anything and therefor it can't optimize anything. A "final" variable is not constant. L.
Mar 20 2007
Lionello Lunesu wrote:Tyler Knott wrote:final int x = 42; Change that :o). AndreiLionello Lunesu Wrote:...which I want, very much!What's the use of "final" for variables? I'm saying "for variables" because for methods the benefit is only too clear.Because the "const" keyword is being repurposed for read-only references to mutable or non-mutable data,we need a new keyword for non-mutable variables.That's my question: do we actually need that? Examples are welcomed. > "final" fills that purpose nicely. If the value of the variable is known at compile time, the compiler can constant-fold away the memory access to that variable for a small speed boost. If the value is only determinable at runtime, that can still allow the compiler to make some optimizations that would not be possible with mutable variables. This is the part I'm not sure about. As with C++'s "const", I don't think the compiler can conclude anything and therefor it can't optimize anything. A "final" variable is not constant.
Mar 20 2007
Andrei Alexandrescu (See Website For Email) wrote:How does this interact with CTFE for variables that are declared as const. With previous versions of the compiler, I know that it would try to constant-fold this code, resulting in an inlined constant from the result of the CTFE: const int abc = kaboom(); That code shouts out to me (and to the compiler) that the kaboom() method MUST be evaluated at compile-time. But this code is not nearly so clear: final int xyz = shabam(); Just because xyz can't be re-bound after its initial assignment doesn't necessarily mean that shabam() should be executed at compile-time. With the new const/final/invariant semantics, how will the compiler know when to perform constant-folding (or, I suppose, "final-folding") on functions? --benjiThis is the part I'm not sure about. As with C++'s "const", I don't think the compiler can conclude anything and therefor it can't optimize anything. A "final" variable is not constant.final int x = 42; Change that :o). Andrei
Mar 20 2007
Benji Smith wrote:Andrei Alexandrescu (See Website For Email) wrote:Undecided at this time. At least "static final" will take care of that - you can't dynamically-initialize statics. AndreiHow does this interact with CTFE for variables that are declared as const. With previous versions of the compiler, I know that it would try to constant-fold this code, resulting in an inlined constant from the result of the CTFE: const int abc = kaboom(); That code shouts out to me (and to the compiler) that the kaboom() method MUST be evaluated at compile-time. But this code is not nearly so clear: final int xyz = shabam(); Just because xyz can't be re-bound after its initial assignment doesn't necessarily mean that shabam() should be executed at compile-time. With the new const/final/invariant semantics, how will the compiler know when to perform constant-folding (or, I suppose, "final-folding") on functions?This is the part I'm not sure about. As with C++'s "const", I don't think the compiler can conclude anything and therefor it can't optimize anything. A "final" variable is not constant.final int x = 42; Change that :o). Andrei
Mar 20 2007
Andrei Alexandrescu (See Website For Email) wrote:Benji Smith wrote:Damn. I always thought "static final" in Java was a sloppy kludge for what should have been "const". --benjiAndrei Alexandrescu (See Website For Email) wrote:Undecided at this time. At least "static final" will take care of that - you can't dynamically-initialize statics. AndreiHow does this interact with CTFE for variables that are declared as const. With previous versions of the compiler, I know that it would try to constant-fold this code, resulting in an inlined constant from the result of the CTFE: const int abc = kaboom(); That code shouts out to me (and to the compiler) that the kaboom() method MUST be evaluated at compile-time. But this code is not nearly so clear: final int xyz = shabam(); Just because xyz can't be re-bound after its initial assignment doesn't necessarily mean that shabam() should be executed at compile-time. With the new const/final/invariant semantics, how will the compiler know when to perform constant-folding (or, I suppose, "final-folding") on functions?This is the part I'm not sure about. As with C++'s "const", I don't think the compiler can conclude anything and therefor it can't optimize anything. A "final" variable is not constant.final int x = 42; Change that :o). Andrei
Mar 20 2007
Benji Smith wrote:Damn. I always thought "static final" in Java was a sloppy kludge for what should have been "const".It is. This is because Java lacks the notion of const for indirectly-referred data, which is the interesting case. Andrei
Mar 20 2007
Andrei Alexandrescu (See Website For Email) wrote:Benji Smith wrote:But presumably that would allocate space for the variable in the data segment. int CTFE_function1(int x) { static final y = CTFE_function2(x); } And that's a really clumsy construction for something which occurs so frequently.Andrei Alexandrescu (See Website For Email) wrote:Undecided at this time. At least "static final" will take care of that - you can't dynamically-initialize statics.How does this interact with CTFE for variables that are declared as const. With previous versions of the compiler, I know that it would try to constant-fold this code, resulting in an inlined constant from the result of the CTFE: const int abc = kaboom(); That code shouts out to me (and to the compiler) that the kaboom() method MUST be evaluated at compile-time. But this code is not nearly so clear: final int xyz = shabam(); Just because xyz can't be re-bound after its initial assignment doesn't necessarily mean that shabam() should be executed at compile-time. With the new const/final/invariant semantics, how will the compiler know when to perform constant-folding (or, I suppose, "final-folding") on functions?This is the part I'm not sure about. As with C++'s "const", I don't think the compiler can conclude anything and therefor it can't optimize anything. A "final" variable is not constant.final int x = 42; Change that :o). Andrei
Mar 23 2007
Benji Smith wrote:Andrei Alexandrescu (See Website For Email) wrote:const would still exist, right? So you'd just keep using "const int x = ...". The _only_ purpose of "final int y" is for code clarity, you don't have to scan a whole function to see what happens to that variable, since nothing will happen to it. L. L.How does this interact with CTFE for variables that are declared as const. With previous versions of the compiler, I know that it would try to constant-fold this code, resulting in an inlined constant from the result of the CTFE: const int abc = kaboom(); That code shouts out to me (and to the compiler) that the kaboom() method MUST be evaluated at compile-time. But this code is not nearly so clear: final int xyz = shabam(); Just because xyz can't be re-bound after its initial assignment doesn't necessarily mean that shabam() should be executed at compile-time. With the new const/final/invariant semantics, how will the compiler know when to perform constant-folding (or, I suppose, "final-folding") on functions?This is the part I'm not sure about. As with C++'s "const", I don't think the compiler can conclude anything and therefor it can't optimize anything. A "final" variable is not constant.final int x = 42; Change that :o). Andrei
Mar 20 2007
Andrei Alexandrescu (See Website For Email) wrote:final int x = 42; Change that :o).Not fair! You meant "const" :) L.
Mar 20 2007
Lionello Lunesu wrote:I've been trying to follow the thread on the const/final/invariant extensions but I have to wonder: how useful are final variables? final int i = 2; This would mean: 'i' cannot be rebound to another value. Right? So this final is actually C++'s const. void func( final int i ) { //i is not const but cannot be set either } Also here, it's like C++'s const, but with the _removed_ benefit of restricting the accepted types. Is this correct? What's the use of "final" for variables? I'm saying "for variables" because for methods the benefit is only too clear. L. PS. I've started a new thread because this isn't a reply to any particular post.Very simple. void Fun(const char[] s) { ... } Cannot modify s's content, but can rebind it. If the programmer wants to not rebind, then she writes: void Fun(final const char[] s) { ... } Andrei
Mar 20 2007
Andrei Alexandrescu (See Website For Email) wrote:Lionello Lunesu wrote:I understand, but what's the point of telling the compiler "I don't want to rebind this variable"? It doesn't seem like an optimization hint. Like telling "func(const int i)" in C++ doesn't actually do anything. You just type more. And if you later want to rebind 'i' you'll have to change the interface. Lose-lose? L.I've been trying to follow the thread on the const/final/invariant extensions but I have to wonder: how useful are final variables? final int i = 2; This would mean: 'i' cannot be rebound to another value. Right? So this final is actually C++'s const. void func( final int i ) { //i is not const but cannot be set either } Also here, it's like C++'s const, but with the _removed_ benefit of restricting the accepted types. Is this correct? What's the use of "final" for variables? I'm saying "for variables" because for methods the benefit is only too clear. L. PS. I've started a new thread because this isn't a reply to any particular post.Very simple. void Fun(const char[] s) { ... } Cannot modify s's content, but can rebind it. If the programmer wants to not rebind, then she writes: void Fun(final const char[] s) { ... }
Mar 20 2007
Lionello Lunesu wrote:Andrei Alexandrescu (See Website For Email) wrote:Many people, including yours truly, appreciate the ability of automatically checking their assumptions or desires. AndreiLionello Lunesu wrote:I understand, but what's the point of telling the compiler "I don't want to rebind this variable"? It doesn't seem like an optimization hint. Like telling "func(const int i)" in C++ doesn't actually do anything. You just type more. And if you later want to rebind 'i' you'll have to change the interface. Lose-lose?I've been trying to follow the thread on the const/final/invariant extensions but I have to wonder: how useful are final variables? final int i = 2; This would mean: 'i' cannot be rebound to another value. Right? So this final is actually C++'s const. void func( final int i ) { //i is not const but cannot be set either } Also here, it's like C++'s const, but with the _removed_ benefit of restricting the accepted types. Is this correct? What's the use of "final" for variables? I'm saying "for variables" because for methods the benefit is only too clear. L. PS. I've started a new thread because this isn't a reply to any particular post.Very simple. void Fun(const char[] s) { ... } Cannot modify s's content, but can rebind it. If the programmer wants to not rebind, then she writes: void Fun(final const char[] s) { ... }
Mar 20 2007
Lionello Lunesu wrote:I understand, but what's the point of telling the compiler "I don't want to rebind this variable"? It doesn't seem like an optimization hint. Like telling "func(const int i)" in C++ doesn't actually do anything. You just type more.Final is handy for a number of things: 1) For class members, it would indicate that a class is not mutable once it's constructed. Probably most classes are like this, and so it's a handy way to document it. 2) Andrei pointed out to me that one can often replace: auto x = initializer; with: final x = initializer; to indicate in a block of code that x will never be reassigned. This aids in understanding the code. It's a style I intend to adopt. 3) For cases like: const char[] s = "hello"; is it s that's not rebindable, or is the data that s is pointing to immutable? This never fails to confuse me (in C++), so I like to be able to say which it is in a clear manner.And if you later want to rebind 'i' you'll have to change the interface. Lose-lose?Although final is specified in the interface, it is not part of the interface. It only affects the implementation of the interface. You'll be able to change it without affecting any users of the interface.
Mar 20 2007
Walter Bright wrote:Lionello Lunesu wrote:And the compiler will have an excellent opportunity to optimize the code (e.g. it can enregister "x" without fear). It's a win-win deal. Believe me. I'm not wearing a tie.I understand, but what's the point of telling the compiler "I don't want to rebind this variable"? It doesn't seem like an optimization hint. Like telling "func(const int i)" in C++ doesn't actually do anything. You just type more.Final is handy for a number of things: 1) For class members, it would indicate that a class is not mutable once it's constructed. Probably most classes are like this, and so it's a handy way to document it. 2) Andrei pointed out to me that one can often replace: auto x = initializer; with: final x = initializer; to indicate in a block of code that x will never be reassigned. This aids in understanding the code. It's a style I intend to adopt.3) For cases like: const char[] s = "hello"; is it s that's not rebindable, or is the data that s is pointing to immutable? This never fails to confuse me (in C++), so I like to be able to say which it is in a clear manner.What still needs to be decided here is whether we want to automatically make s final too when seen in a data definition. This is a hack for existing code's sake, and also because many people actually mean to make final const strings more often than just nonfinal const strings. Probably final can be inferred via simple flow analysis at low or no extra cost, which would please everyone. Andrei
Mar 20 2007
On Tue, 20 Mar 2007 14:03:34 -0700, Andrei Alexandrescu (See Website For Email) wrote:Walter Bright wrote:Now I know (really) that new keywords are *to be avoided at all costs*, but this looks nice to me ... literal char[] s = "hello"; meaning that neither 's' can be changed nor can the data 's' points to. -- Derek (skype: derek.j.parnell) Melbourne, Australia "Justice for David Hicks!" 21/03/2007 10:17:22 AMLionello Lunesu wrote:And the compiler will have an excellent opportunity to optimize the code (e.g. it can enregister "x" without fear). It's a win-win deal. Believe me. I'm not wearing a tie.I understand, but what's the point of telling the compiler "I don't want to rebind this variable"? It doesn't seem like an optimization hint. Like telling "func(const int i)" in C++ doesn't actually do anything. You just type more.Final is handy for a number of things: 1) For class members, it would indicate that a class is not mutable once it's constructed. Probably most classes are like this, and so it's a handy way to document it. 2) Andrei pointed out to me that one can often replace: auto x = initializer; with: final x = initializer; to indicate in a block of code that x will never be reassigned. This aids in understanding the code. It's a style I intend to adopt.3) For cases like: const char[] s = "hello"; is it s that's not rebindable, or is the data that s is pointing to immutable? This never fails to confuse me (in C++), so I like to be able to say which it is in a clear manner.What still needs to be decided here is whether we want to automatically make s final too when seen in a data definition. This is a hack for existing code's sake, and also because many people actually mean to make final const strings more often than just nonfinal const strings. Probably final can be inferred via simple flow analysis at low or no extra cost, which would please everyone.
Mar 20 2007
"Walter Bright" <newshound digitalmars.com> wrote in message news:etpb8a$bfe$1 digitalmars.com...Lionello Lunesu wrote:I understand, but what's the point of telling the compiler "I don't want to rebind this variable"? It doesn't seem like an optimization hint. Like telling "func(const int i)" in C++ doesn't actually do anything. You just type more.Final is handy for a number of things: 1) For class members, it would indicate that a class is not mutable once it's constructed. Probably most classes are like this, and so it's a handy way to document it.2) Andrei pointed out to me that one can often replace: auto x = initializer; with: final x = initializer; to indicate in a block of code that x will never be reassigned. This aids in understanding the code. It's a style I intend to adopt.OK, "final x" means that you can skip big pieces of code, knowing that "x" will not change.3) For cases like: const char[] s = "hello"; is it s that's not rebindable, or is the data that s is pointing to immutable? This never fails to confuse me (in C++), so I like to be able to say which it is in a clear manner.Will "const" imply "final"? Hm, guess not, since you'll still want to write "const char* x" and reassign 'x' later. But now I'm thinking too much C++.Ah, that alone makes it a lot more practical than (half of) C++'s const. Good one. Some good points. Thanks. L.And if you later want to rebind 'i' you'll have to change the interface. Lose-lose?Although final is specified in the interface, it is not part of the interface. It only affects the implementation of the interface. You'll be able to change it without affecting any users of the interface.
Mar 20 2007
Walter Bright wrote:Lionello Lunesu wrote:Same as C++ here then: the declaration void f(int); is identical to void f(int const); though in the corresponding definition the implementation gets checking that it doesn't alter its local copy of the int if it uses const qualification. It's quite legal (and quite common) to declare void f(int); and then define f as void f(int const value) { // ... code here can't accidentally change the local value } -- JamesAnd if you later want to rebind 'i' you'll have to change the interface. Lose-lose?Although final is specified in the interface, it is not part of the interface. It only affects the implementation of the interface. You'll be able to change it without affecting any users of the interface.
Mar 20 2007
Lionello Lunesu wrote:I've been trying to follow the thread on the const/final/invariant extensions but I have to wonder: how useful are final variables? [...]If you spend any amount of time writing Java code, you can appreciate 'final' fairly easily. In Java, String is immutable, so a final String is like a const std::string. On the other hand, Java provides StringBuilder for mutable operations on strings. It's a little like std::stringstream, but not quite. // Create a new StringBuilder and reserve a buffer of at least 48 chars StringBuilder s = new StringBuilder(48); s.append("Hello,"); ... s = methodReturningNewStringBuilder(); ... s.append(" world!"); // s.toString() == "Hello, world!"? Maybe, maybe not final StringBuilder s = new StringBuilder(48); s.append("Hello,"); ... s = methodReturningNewStringBuilder(); // Error ... s.append(" world!"); // s.toString() == "Hello, world!"? Provably! What you can't do in Java is declare a const or invariant StringBuilder. While this example is a little contrived, once you start talking about file streams and JDBC Statements and ResultSets, knowing whether a variable has been rebound becomes a very big deal. That is, just declaring a few things 'final' can eliminate a whole host of potential bugs and let you focus on the parts of the code that really matter. Dave
Mar 21 2007