| 470 | |
| 471 | == Use of lambda notation, inner class extraction |
| 472 | Java has the arrow notation (x,y) -> methodcall(x,y) to write lambda expressions. |
| 473 | This notation can also be translated. This feature is especially useful when pulling inner classes out of a parent class, to pass parent class functions over to the pulled-out class. |
| 474 | For example, suppose you have a class like this |
| 475 | |
| 476 | {{{ |
| 477 | class Parent { |
| 478 | public void f() {...} |
| 479 | public String g(int x) {...} |
| 480 | |
| 481 | |
| 482 | public X h() { |
| 483 | .... |
| 484 | return new X{ |
| 485 | f() |
| 486 | g(1) |
| 487 | } |
| 488 | } |
| 489 | }}} |
| 490 | |
| 491 | The "new X" call has to be converted into an explicit class, but somehow it needs to be able to call f and g. We suggest to do it like this: |
| 492 | |
| 493 | {{{ |
| 494 | class MyX extends X { |
| 495 | public MyX(Runnable f, Function<Integer,String> g) { |
| 496 | f.run(); |
| 497 | g.apply(1); |
| 498 | } |
| 499 | } |
| 500 | |
| 501 | class Parent { |
| 502 | public void f() {...} |
| 503 | public String g(int x) {...} |
| 504 | |
| 505 | |
| 506 | public X h() { |
| 507 | .... |
| 508 | return new MyX{()->f,(x)->g(x)); |
| 509 | } |
| 510 | |
| 511 | |