jface.snippets.Snippet031 : Win32 Exception
Frank Benoit
keinfarbton at googlemail.com
Wed May 21 01:20:07 PDT 2008
A potential problem might be the access to local variables that were no
more alive.
A Java local variable with keyword "final" is like a big red warning for
me. When porting this to D, it needs special care to examing the
accesses to this variable. In Java the "final" makes this variable to be
available for anonymouse classes even if the method exits. This feature
is in D2 available as "full closure". In D1, you need to take for this
on your own.
In your code, there is this "final Table table = new Table(..." which is
accessed from the anonymous Listener directly.
Possible solutions:
1.) create a local variable in the anonymouse class for the table. Call
this variable eg. "table_". Create a constructor [1] and pass it table
as "new class(table) Listener {...". Replace all "table" accesses in the
class with "table_".
The resulting code is verbose and this is error-prune, because there is
no compiler supported check if you missed one of the problematic accesses.
2.) Make a named class outside the local scope
This will immediatly show you the problematic accesses. But still the
code will be verbose.
3.) use the dgListener template function. you can move the handleEvent
function where you want and you do not need the verbose extra variable
saving.
[1] initializing the variable directly with the outer var does create
new problems. The compiler has to choose for a context pointer for this
class. Either the context of the surrounding method or the "this" of the
surrounding class. If you initialize the anonymouse class members
directly the compiler will choose the methods context and an attemp to
access outer member after the surrounded method has exited, => crash.
More information about the Digitalmars-d-dwt
mailing list