DWT2

Steven Schveighoffer schveiguy at yahoo.com
Mon Apr 11 05:20:41 PDT 2011


On Sun, 10 Apr 2011 07:09:52 -0400, Jacob Carlborg <doob at me.com> wrote:

> On 2011-04-08 16:45, Steven Schveighoffer wrote:
>> On Fri, 08 Apr 2011 10:36:39 -0400, Andrej Mitrovic
>> <andrej.mitrovich at gmail.com> wrote:
>>
>>> On 4/8/11, Bruno Medeiros <brunodomedeiros+spam at com.gmail> wrote:
>>>> On 05/04/2011 04:54, Andrej Mitrovic wrote:
>>>>> You have to be kidding me. Who writes code like this?
>>>>>
>>>>> http://i.imgur.com/BBQde.png
>>>>
>>>> What's wrong with it? Do you mean the anonymous classes with all the
>>>> fields and contructors for passing the variables in the enclosing  
>>>> scope?
>>>>
>>>> --
>>>> Bruno Medeiros - Software Engineer
>>>>
>>>
>>> I don't understand the need to create huge anonymous classes. If
>>> you're going to create or override so many methods, you might as well
>>> create a new class instead of embedding one in the call.
>>>
>>> Maybe that's a standard way of doing it in Java, but I'd never code
>>> like this. Maybe it's because I don't depend on an IDE, and like to
>>> keep my code clean. I'm not a fan of deeply nested code.
>>
>> I think the reason it looks like this is because Java does not have
>> delegates. Anonymous delegates are much easier to read, and also do not
>> take up heap space.
>>
>> If anything, I'd say a very worthwhile project would be to change all
>> the callback interfaces and anonymous classes to delegates.
>>
>> -Steve
>
> DWT already have a way to pass a delegate to a function expecting an  
> interface. It will create a class the implements the interface and then  
> delegate the call to, in this case "handleEvent", to the delegate. Have  
> a look at the bottom of:  
> http://dsource.org/projects/dwt/wiki/DiffToOriginal
>

I meant it should be redesigned so the delegate is the only way.  It seems  
to me like the reason for using anonymous classes for callbacks is because  
Java did not have delegates.

I'd replace any requirements for certain interfaces with a requirement for  
a delegate.  Delegates are cheap to pass around (no heap allocation  
required), and easier to create in D than anonymous classes.

It's a one-for-one swap when an interface in DWT just has one function,  
and ones that have multiple functions could probably be either redesigned  
into multiple delegates, or use template functions to extract delegates.

-Steve


More information about the Digitalmars-d mailing list