www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - final variable

reply nobody <nobody_member pathlink.com> writes:
Hallo,

the following programm run fine:

import std.stdio;

class Water {
final char[] water="H2O";
}

int main( char[][] arg ) {
Water water = new Water;
writefln("Water: %s",water.water);
water.water="H2O2";  // why not an error ???
writefln("water: %s",water.water);
return 0;
}

Why can i override water with H2O2.
I think water is constant from the keyword final .
Dec 30 2005
next sibling parent "Jarrett Billingsley" <kb3ctd2 yahoo.com> writes:
"nobody" <nobody_member pathlink.com> wrote in message 
news:dp311j$1r83$1 digitaldaemon.com...
 Hallo,

 the following programm run fine:

 import std.stdio;

 class Water {
 final char[] water="H2O";
 }

 int main( char[][] arg ) {
 Water water = new Water;
 writefln("Water: %s",water.water);
 water.water="H2O2";  // why not an error ???
 writefln("water: %s",water.water);
 return 0;
 }

 Why can i override water with H2O2.
 I think water is constant from the keyword final .
Because D is not Java, and to make a constant variable in D, you use "const."
Dec 30 2005
prev sibling next sibling parent reply Chris Sauls <ibisbasenji gmail.com> writes:
nobody wrote:
 Hallo,
 
 the following programm run fine:
 
 import std.stdio;
 
 class Water {
 final char[] water="H2O";
 }
 
 int main( char[][] arg ) {
 Water water = new Water;
 writefln("Water: %s",water.water);
 water.water="H2O2";  // why not an error ???
 writefln("water: %s",water.water);
 return 0;
 }
 
 Why can i override water with H2O2.
 I think water is constant from the keyword final .
Its a simple mistake, especially coming from a Java background (which you don't specify, its just what makes sense). In D the 'final' keyword is used to make a class which cannot be subclassed. For example, this would error: In terms of class members... I'm not sure that final does anything, although it might make it so a method cannot be overriden. For example: For what you want, use the 'const' keyword, like so: -- Chris Sauls
Dec 30 2005
next sibling parent Manfred Nowak <svv1999 hotmail.com> writes:
Chris Sauls wrote:

[...]


[...] Nice idea, but sadly its neither documented nor implemented this way. -manfred
Dec 30 2005
prev sibling parent reply James Dunne <james.jdunne gmail.com> writes:
 Its a simple mistake, especially coming from a Java background (which 
 you don't specify, its just what makes sense).  In D the 'final' keyword 
 is used to make a class which cannot be subclassed.  For example, this 
 would error:
 


 
I believe you mean 'final class A {}' and 'class B : A {}', but final does not apply to classes, only methods within classes. I don't believe there is such thing as a final/sealed class in D, and frankly I don't see the need for such silliness.
 In terms of class members... I'm not sure that final does anything, 
 although it might make it so a method cannot be overriden.  For example:
 






 final A.foo

 
"function B.foo cannot override final function A.foo"
 For what you want, use the 'const' keyword, like so:
 






 
 -- Chris Sauls
Correct.
Dec 30 2005
parent "John C" <johnch_atms hotmail.com> writes:
"James Dunne" <james.jdunne gmail.com> wrote in message 
news:dp5as6$j29$1 digitaldaemon.com...
 Its a simple mistake, especially coming from a Java background (which you 
 don't specify, its just what makes sense).  In D the 'final' keyword is 
 used to make a class which cannot be subclassed.  For example, this would 
 error:



I believe you mean 'final class A {}' and 'class B : A {}', but final does not apply to classes, only methods within classes. I don't believe there is such thing as a final/sealed class in D, and frankly I don't see the need for such silliness.
I'm pretty sure 'final' used to work on classes. But I have struggled to find cases where it would be appropriate to seal a class. Off topic, but related, there are good reasons for hiding classes ('private class A {}'), but we can't do that either (unless they're nested).
 In terms of class members... I'm not sure that final does anything, 
 although it might make it so a method cannot be overriden.  For example:







 A.foo

"function B.foo cannot override final function A.foo"
 For what you want, use the 'const' keyword, like so:








 -- Chris Sauls
Correct.
Dec 31 2005
prev sibling parent nobody <nobody_member pathlink.com> writes:
Ok, thank you, 

i will write it in my tutorial, that it gives no final variable's, 
because D has the keyword const for this. 



In article <dp311j$1r83$1 digitaldaemon.com>, nobody says... 
 
Hallo, 
 
the following programm run fine: 
 
import std.stdio; 
 
class Water { 
final char[] water="H2O"; 
} 
 
int main( char[][] arg ) { 
Water water = new Water; 
writefln("Water: %s",water.water); 
water.water="H2O2";  // why not an error ??? 
writefln("water: %s",water.water); 
return 0; 
} 
 
Why can i override water with H2O2. 
I think water is constant from the keyword final . 
 
 
 
 
Dec 31 2005