PROPOSAL: Return 'this' (update)

Marek Kozieł develop4lasu at gmail.com
Mon Mar 30 15:43:44 PDT 2009


AUTHOR: Lasu aka Marek Kozieł


OVERVIEW

FEATURE SUMMARY:
It allows the method to return reference to 'this' object.

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.

MAJOR DISADVANTAGE:
Returned value cannot be changed while inheritance, also other objects
cannot be returned (in my opinion it's benefit).

ALTERNATIVES:
Self-bounded generics, This type, change returned type for every
method on each inheritance.





EXAMPLES

SIMPLE/ADVANCED EXAMPLE:
public class Builder {

 String text = "";

 public this add (char c){text+=c;}

 public this add (String c){
   if (c==null){
     text +="null";
     return; // this will be returned
   }
   text+=c;
 }

 public static void test(Builder builder) {
   builder.add('.').add("test()").add(':');
 }

}

package test;

public class ReturnThis {

 static class A { public this test() { } }

 static class B extends A { }

 static Class<?> getReturnedType(Class<?> classs) {
   try {
     return classs.getMethod("test").getReturnType();
   } catch (SecurityException e) {
     e.printStackTrace();
   } catch (NoSuchMethodException e) {
     e.printStackTrace();
   }
   return null;
 }

 public static void main(String[] args) {
   System.out.println(getReturnedType(A.class));
   // will print: class returnThis.ReturnThis$A

   System.out.println(getReturnedType((new A()).getClass()));
   // will print: class returnThis.ReturnThis$A

   System.out.println(getReturnedType(B.class));
   // will print: class returnThis.ReturnThis$B

   System.out.println(getReturnedType((new B()).getClass()));
   // will print: class returnThis.ReturnThis$B
 }
}


DETAILS

SPECIFICATION:
JLS 8.4

ResultType:
  Type
  void
  this

A method declaration either specifies the type of value that the
method returns, uses the keyword void to indicate that the method does
not return a value, or uses the keyword this to indicate that the
method return the reference to 'this' object.

Keyword this cannot occurs if method is static.




JLS 14.17

ReturnStatement:
  return Expressionopt ;

  A return statement with no Expression must be contained in the body
of a method that is declared, using the keyword void, not to return
any value (§8.4), or in the body of a method that is declared, using
the keyword this , to return this reference (§8...),or in the body of
a constructor (§8.8).


Method Overriding:
  Method with 'this' as ResultType can be override only with method
with the same ResultType (proposal in this form can be, however,
extended to work with backward covariant return types in future)



COMPILATION:
Method just before exiting from it's scope returns 'this'.
Returned object type is ALWAYS actual object type.
Method in compiled form have special 'this' type as ResultType, which
is exchanged during loading to be actual object type:

interface A{ this sample(); }
interface B extends A { }
class C implements B { this sample(){...}; }
So compiled classes contain only methods with A and C, but after
loading they are extended to ensure that Method.getReturnType() will
be always actual object type.

TESTING:
  Most efforts need to be focused on ensuring that
Method.getReturnType() will work in a correct way.

LIBRARY SUPPORT:
No.

REFLECTIVE APIS:
No: From this perspective methods are simply overridden in runtime.

OTHER CHANGES:
Javadoc:
return type is visible as 'this' and linked to current class.

MIGRATION:
None.

COMPATIBILITY
Backward: full;
Forward: can be handled like other void type to be partial compatibile.

REFERENCES
  Bug: 6479372
http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6479372

http://java.net/cs/user/view/cs_msg/37432

http://lasu2string.blogspot.com/2009/03/return-this-proposal.html



More information about the coin-dev mailing list