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

Marek Kozieł develop4lasu at gmail.com
Wed Mar 25 14:59:06 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.

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 (modifications)

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 for return cannot occurs if method is static.

JLS 14.17 (modifications)

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).

COMPILATION: Method just before exiting from it's scope returns 'this'. Exceptions work same as before. Returned object type is actual object type.

TESTING: Normal way.

LIBRARY SUPPORT: No.

REFLECTIVE APIS: No.

OTHER CHANGES: None.

MIGRATION: None.

COMPATIBILITY Backward: full. Forward: Only jars.

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

PS. Maybe this Joe consider as 'proposal'. Any questions?

-- Pozdrowionka. / Regards. Lasu aka Marek Kozieł

http://lasu2string.blogspot.com/



More information about the coin-dev mailing list