digitalmars.D.learn - final variable
- nobody (15/15) Dec 30 2005 Hallo,
- Jarrett Billingsley (4/19) Dec 30 2005 Because D is not Java, and to make a constant variable in D, you use
- Chris Sauls (23/43) Dec 30 2005 Its a simple mistake, especially coming from a Java background (which yo...
- Manfred Nowak (5/7) Dec 30 2005 [...]
- James Dunne (6/36) Dec 30 2005 I believe you mean 'final class A {}' and 'class B : A {}', but final
- John C (6/42) Dec 31 2005 I'm pretty sure 'final' used to work on classes. But I have struggled to...
- nobody (4/29) Dec 31 2005 Ok, thank you,
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
"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
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
Chris Sauls wrote: [...][...] Nice idea, but sadly its neither documented nor implemented this way. -manfred
Dec 30 2005
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 SaulsCorrect.
Dec 30 2005
"James Dunne" <james.jdunne gmail.com> wrote in message news:dp5as6$j29$1 digitaldaemon.com...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).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: A.foo"function B.foo cannot override final function A.foo"For what you want, use the 'const' keyword, like so: -- Chris SaulsCorrect.
Dec 31 2005
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