www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - final class member variable NOT LIKE JAVA!

reply "Garett Bass" <garettbass studiotekne.com> writes:
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 
Jan 01 2006
next sibling parent Frank Benoit <frank_DELETE_ _DELETE_drealtime.com> writes:
See the in digitalmars.D.learn ng. 
There was this "final variable" discussion just yesterday.

Frank

-- 
D goes real-time: http://www.drealtime.com
Jan 01 2006
prev sibling parent "Chris Miller" <chris dprogramming.com> writes:
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.
Jan 01 2006