Return 'this' proposal

Marek Kozieł develop4lasu at gmail.com
Sun Mar 15 14:34:27 PDT 2009


AUTHOR: Lasu aka Marek Kozieł

GENESIS

It's a simplified 'This' type problem, which seems to be too much
complicated as for now and need more analyses. Construction is designed not
to interact with possibility that 'This' type will be introduced.
OVERVIEW

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

MAJOR ADVANTAGE:
Simplification of return this; statement.
'void' can be easy replaced with 'this'.

MAJOR BENEFIT(s):It would prevent NFP, in a described situation, and make
some 'builder' like interfaces look really clear and simple.

MAJOR DISADVANTAGE:
It's a change in language. Depending on the way that it would be compiled to
byte-code, it determine compatibility(here may lie some problem that I do
not know about).

ALTERNATIVES:
Self-bounded generics.


EXAMPLES

SIMPLE/ADVANCED EXAMPLE:
public class Builder
{

  public this add (char c){...}

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

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

}
DETAILS

SPECIFICATION:
JLS 8.4:
http://java.sun.com/docs/books/jls/third_edition/html/classes.html#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 called object.

Keyword this cannot occurs if method is static.


JLS 14.17:
http://java.sun.com/docs/books/jls/third_edition/html/statements.html#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).

COMPILATION:
Returned 'void' should be replaced with 'this', no more no less.

TESTING:
Normal way.

LIBRARY SUPPORT:
No.

REFLECTIVE APIS:
No.

OTHER CHANGES:

I wander what object should represent 'this' return type in:
Method.getReturnType(); (this.getObject() ?)

MIGRATION:
None.

COMPATIBILITY
Backward: full.

REFERENCES
Bug: 6479372: http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6479372
http://java.net/cs/user/view/cs_msg/37432
Return 'this' proposal :
http://lasu2string.blogspot.com/2009/03/return-this-proposal.html

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

http://lasu2string.blogspot.com/



More information about the coin-dev mailing list