Changes between Version 158 and Version 159 of j2p


Ignore:
Timestamp:
11/13/24 13:58:38 (12 days ago)
Author:
wouter
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • j2p

    v158 v159  
    468468* various small fixes, to work around a few restrictions of the translator.
    469469
     470
     471== Use of lambda notation, inner class extraction
     472Java has the arrow notation (x,y) -> methodcall(x,y) to write lambda expressions.
     473This 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.
     474For example, suppose you have a class like this
     475
     476{{{
     477class 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
     491The "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{{{
     494class MyX extends X {
     495  public MyX(Runnable f, Function<Integer,String> g) {
     496    f.run();
     497    g.apply(1);
     498  }
     499}
     500
     501class 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
    470512== FAQs
    471513||=Question=||=Explanation=||