130 | | ===== Workaround |
| 130 | ==== Example workaround |
| 131 | Java has the arrow notation (x,y) -> methodcall(x,y) to write lambda expressions. |
| 132 | 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. |
| 133 | For example, suppose you have a class like this |
| 134 | |
| 135 | {{{ |
| 136 | class Parent { |
| 137 | public void f() {...} |
| 138 | public String g(int x) {...} |
| 139 | |
| 140 | |
| 141 | public X h() { |
| 142 | .... |
| 143 | return new X{ |
| 144 | f() |
| 145 | g(1) |
| 146 | } |
| 147 | } |
| 148 | }}} |
| 149 | |
| 150 | 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: |
| 151 | |
| 152 | {{{ |
| 153 | class MyX extends X { |
| 154 | public MyX(Runnable f, Function<Integer,String> g) { |
| 155 | f.run(); |
| 156 | g.apply(1); |
| 157 | } |
| 158 | } |
| 159 | |
| 160 | class Parent { |
| 161 | public void f() {...} |
| 162 | public String g(int x) {...} |
| 163 | |
| 164 | |
| 165 | public X h() { |
| 166 | .... |
| 167 | return new MyX{()->f(),(x)->g(x)); |
| 168 | } |
| 169 | }}} |
| 170 | |
| 171 | ===== Special cases |
446 | | == Use of lambda notation, inner class extraction |
447 | | Java has the arrow notation (x,y) -> methodcall(x,y) to write lambda expressions. |
448 | | 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. |
449 | | For example, suppose you have a class like this |
450 | | |
451 | | {{{ |
452 | | class Parent { |
453 | | public void f() {...} |
454 | | public String g(int x) {...} |
455 | | |
456 | | |
457 | | public X h() { |
458 | | .... |
459 | | return new X{ |
460 | | f() |
461 | | g(1) |
462 | | } |
463 | | } |
464 | | }}} |
465 | | |
466 | | 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: |
467 | | |
468 | | {{{ |
469 | | class MyX extends X { |
470 | | public MyX(Runnable f, Function<Integer,String> g) { |
471 | | f.run(); |
472 | | g.apply(1); |
473 | | } |
474 | | } |
475 | | |
476 | | class Parent { |
477 | | public void f() {...} |
478 | | public String g(int x) {...} |
479 | | |
480 | | |
481 | | public X h() { |
482 | | .... |
483 | | return new MyX{()->f(),(x)->g(x)); |
484 | | } |
485 | | }}} |