implicit 'return this' JCP suggestion

Scott Morgan scott at adligo.com
Wed Jul 21 23:22:23 UTC 2021


Hi All,

  I have a JCP suggestion (between my first and 2nd signatures below), and
I am wondering if anyone on this list can submit it on my behalf, or if
they are interested in collaboration on it.

Cheers and TIA!,
Scott

Summary

-------



  Implicit return of this;



  Provide better syntactic sugar for method chaining in Java, through
allowing the 'this' keyword to be used

in the return class slot of Java methods.  In addition methods marked as
returning 'this' would implicitly return

this with out the explicit 'return this;' statement at the end of the
method.  For example;



public interface MyInterface {

  this setFoo(String foo);

}



public class MyClass implements MyInterface {

  private String foo;



  public this setFoo(String foo) {

    this.foo = foo;

    //note the implicit return this;

  }

}



public class MyChildClass extends MyClass  {

// NO NEED FOR THIS METHOD NOW YEA, it now returns MyChildClass!

//  public MyChildClass setFoo(String foo) {

//    super.setFoo(foo);

//    return this;

//  }

}



Goals

-----

  The primary goal is to make Java easier to use, less typing required and
improved readability.

Removal of repetitive code.



Non-Goals

---------



  This is not designed to promote method chaining, as that is subjective.
However for the developers

who like method chaining, it should improve their experience with Java.



Success Metrics

---------------



  Take a vote of JCP members to see if they think this is a good idea, for

developer productivity with the language.



Motivation

----------



  I have been spending a lot of time overriding methods in child classes in
order to

make all of the methods return the same type.  This creates a lot of nearly
identical

code, violating the DRY principles (don't repeat yourself).  The only
difference is the

return type in the leaf most class of the class parent child tree.



Description

-----------



I'm hoping someone else will implement this JEP.  However I think

it would be fairly straight forward step in the java compilers.

When the compiler encounters a 'this' in the return type slot

it would return the current class.  For all subclasses and

implementations the compiler would have bytecode generated that would

call the super method and then return this.



Alternatives

------------



I haven't considered any alternatives.  The current work around is pretty
repetitive.





Testing

-------

  The parent child classes in the Summary of this JEP should suffice.



Risks and Assumptions

---------------------

  I don't see any risks.  I am assuming that this will NOT require a change
to any bytecode,

only a change to the language.



Dependencies

------------

  This would require a change to all Java compliers.

-- 
Regards,
Scott Morgan
President & CEO
Adligo Inc
http://www.adligo.com
https://www.linkedin.com/in/scott-morgan-21739415
A+ Better Business Bureau Rating
<https://www.bbb.org/chicago/business-reviews/computer-software-publishers-and-developers/adligo-inc-in-chicago-il-88381256>
https://github.com/adligo

By Appointment Only:
1-866-968-1893 Ex 101
scott at adligo.com
skype:adligo1?call
Send Me Files Securely:
*https://www.sendthisfile.com/f.jsp?id=ewOnyeFQM18IDRf7MMIdolfI
<https://www.sendthisfile.com/f.jsp?id=ewOnyeFQM18IDRf7MMIdolfI>*


More information about the discuss mailing list