PROPOSAL: Return 'this' (original) (raw)

Gabriel Belingueres belingueres at gmail.com
Thu Mar 26 11:30:45 PDT 2009


Wouldn't it be simpler to add a Pascal-like "with" statement? This would solve at least the method chaining issue:

with(object) { method1(); metohd2(); ... methodN(); }

2009/3/26, Marek Kozieł <develop4lasu at gmail.com>:

2009/3/26 Tom Hawtin <Thomas.Hawtin at sun.com>: > Marek Kozieł wrote: > >> MAJOR ADVANTAGE: >> Simplification of "return this" statement. >> 'void' can be easy replaced with 'this'. >> >> MAJOR BENEFIT(s): It would prevent NullPointerException, and make some >> 'builder' like interfaces look really clear and simple. > > My problem with this is that it is an attempt to fix a hack due to a > language problem with a language hack. > > The underlying problem is that Java does not have a concise syntax for > builders. The usual hack is for builders to return this and clients to use > method chaining. This solution makes the hack easier. A better solution > would be direct, explicit support for builders in some form. > > Also the variant of this proposal that client-side reinterprets returning > void to return this, would interfere to some extent with covariantly > returning void when overriding a return of Void. > > Tom Hawtin >

This is some point. I do not like it just because in need to be served outside, which increase interactions. Notice that this will work totally in unexpected way when: we have a.jar & b.jar while both are compiled and b.jar use a.jar, than some one change (in a.jar ) interface A{ this some(); } to: interface A{ A some(); } because he want to return other object... this would work in pretty unexpected way. BTW. Did you wander if Interfaces / abstract classes should be final (with explicit declared classes/interfaces available for inheritance), like: final interface A allow Ab, Ac{...} this was my idea connected to inheritance security. -- Pozdrowionka. / Regards. Lasu aka Marek Kozieł http://lasu2string.blogspot.com/



More information about the coin-dev mailing list