Thread overview
final class member variable NOT LIKE JAVA!
Jan 02, 2006
Garett Bass
Jan 02, 2006
Frank Benoit
Jan 02, 2006
Chris Miller
January 02, 2006
Expecting final to work as it does in Java, I wrote the following:

class Bar {
    int i = 2;
}

class Foo {
    final Bar bar;

    this(Bar bar) { this.bar = bar; }
}

You can imagine my surprise when I was allowed to do the following:

int main(char[][] args) {
    Foo foo = new Foo;

    foo.bar = new Bar; // final should not allow this!

    return 0;
}

I worked around it by creating a property method that only permits 'get' access to bar, but it was so much more elegant when it was just final Bar bar.

Oh well,
Garett


January 02, 2006
See the in digitalmars.D.learn ng. There was this "final variable" discussion just yesterday.

Frank

-- 
D goes real-time: http://www.drealtime.com
January 02, 2006
On Sun, 01 Jan 2006 21:06:57 -0500, Garett Bass <garettbass@studiotekne.com> wrote:

> Expecting final to work as it does in Java, I wrote the following:
>
> class Bar {
>     int i = 2;
> }
>
> class Foo {
>     final Bar bar;
>
>     this(Bar bar) { this.bar = bar; }
> }
>
> You can imagine my surprise when I was allowed to do the following:
>
> int main(char[][] args) {
>     Foo foo = new Foo;
>
>     foo.bar = new Bar; // final should not allow this!
>
>     return 0;
> }
>
> I worked around it by creating a property method that only permits 'get'
> access to bar, but it was so much more elegant when it was just final Bar
> bar.
>
> Oh well,
> Garett
>

Use const instead.