Note: This website is archived. For up-to-date information about D projects and development, please visit wiki.dlang.org.

Changes between Version 1 and Version 2 of anonymous_classes

Show
Ignore:
Author:
keinfarbton (IP: 217.228.149.107)
Timestamp:
10/23/06 23:03:27 (18 years ago)
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • anonymous_classes

    v1 v2  
    11= Anonymous class = 
    22 
    3 == Problem 1: not supported in D == 
    4 Actually (DMD 0.169), anonymous classes are not working.  
     3In general, D implements anonymous classes like java does it. The difference is access to final variables for the instanciating method. In Java the anonymous class can access local variable or parameter of the instanciating method, if they are final. This is in fact like they are constant values in the anonymous class. In D this would compile without error, but has another semantic. In D, the anonymouse class would get the context pointer of the instanciating method. If the method ends the local vars of the method do not exist any longer. But the reference to the created object is often stored. If the object is later called, and tries to access the local var of the instanciating method, the program will crash. 
    54 
    6 == Problem 2: Every object inherits from JObject == 
    7 Each ported object shall inherit from JObject. In D and Java every object inherits from Object. For ported classes this is more complicated. Javas Object and Ds Object do not have the same methods.  
     5A way to solve this is, to make fields in the anonymous class, which store the values from the instanciating method, in the moment of creation. That implies, TioPort has to find all those variables and add matching parameters in the ctor, and fields into the class. The ctor must be created and copy the values into the fields. 
    86 
    9  
    10 {{{ 
    11  new Listener(){ 
    12  } 
    13 }}} 
    14  
    15 Is Listener a interface of class? If we know it is a class, everything is OK. If Listener is an interface, the class JObject needs to be added as a base class. 
     7Rewriting the class to an explicit inner class would not be necessary, but make no difference. In developement of TioPorts, this helps to find all references to method local variables, because the D compiler will throw an error after the rewrite if the var was not detected and supplied as field.