Thread overview
final variable
Dec 30, 2005
nobody
Dec 30, 2005
Chris Sauls
Dec 31, 2005
Manfred Nowak
Dec 31, 2005
James Dunne
Dec 31, 2005
John C
Dec 31, 2005
nobody
December 30, 2005
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 .




December 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."


December 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:

# final class A {}
# class A : B {} // <-- error: cannot sublcass final A

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:

# class A {
#   final int foo () { return 0; }
# }
#
# class B : A {
#   override int foo () { return 1; } // <-- error: cannot override final A.foo
# }

For what you want, use the 'const' keyword, like so:

# class A {
#   const int foo = 1;
# }
#
# A a = new A;
# a.foo = 2; // <-- error: cannot assign to const A.foo

-- Chris Sauls
December 31, 2005
Chris Sauls wrote:

[...]
> # final class A {}
> # class A : B {} // <-- error: cannot sublcass final A
[...]

Nice idea, but sadly its neither documented nor implemented this way.

-manfred
December 31, 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:
> 
> # final class A {}
> # class A : B {} // <-- error: cannot sublcass final A
> 

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:
> 
> # class A {
> #   final int foo () { return 0; }
> # }
> #
> # class B : A {
> #   override int foo () { return 1; } // <-- error: cannot override final A.foo
> # }
> 

"function B.foo cannot override final function A.foo"

> For what you want, use the 'const' keyword, like so:
> 
> # class A {
> #   const int foo = 1;
> # }
> #
> # A a = new A;
> # a.foo = 2; // <-- error: cannot assign to const A.foo
> 
> -- Chris Sauls

Correct.
December 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 .
> 
> 
> 
> 



December 31, 2005
"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:
>>
>> # final class A {}
>> # class A : B {} // <-- error: cannot sublcass final A
>>
>
> 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:
>>
>> # class A {
>> #   final int foo () { return 0; }
>> # }
>> #
>> # class B : A {
>> #   override int foo () { return 1; } // <-- error: cannot override final
>> A.foo
>> # }
>>
>
> "function B.foo cannot override final function A.foo"
>
>> For what you want, use the 'const' keyword, like so:
>>
>> # class A {
>> #   const int foo = 1;
>> # }
>> #
>> # A a = new A;
>> # a.foo = 2; // <-- error: cannot assign to const A.foo
>>
>> -- Chris Sauls
>
> Correct.