[patterns] reconsidering how the created variable name binds
Reinier Zwitserloot
reinier at zwitserloot.com
Sun Dec 29 03:13:21 UTC 2019
On Sun, 29 Dec 2019 at 00:21, Remi Forax <forax at univ-mlv.fr> wrote:
>
> and obviously you want to transform it to:
>
> @Override
> public boolean equals(Object obj) {
> if (!(obj instanceof Point p)) {
> return false;
> }
> return x == p.x && y == p.y;
> }
>
>
this works in any proposal as:
return obj instanceof Point p && x == p.x && y == p.y;
but more convoluted equals methods don't get the luxury of just mashing all
the conditions into a single chain of boolean and expressions unless helper
methods are involved – but note that proper equals implementations usually
use a construct similar to `if (obj.getClass() != this.getClass()) .... –
and are generated by IDEs or valhalla or lombok, and thus don't make for a
compelling case in my book. The general example stands, however. (If some
expression is NOT of some type, do some fairly simple code (return a
default value or throw something usually), then continue on without an
indent for the common case, where the expression is instanceof that type,
and have a varName available of the appropriate type to avoid any casts).
More information about the amber-dev
mailing list