Lambda & annonymous classes


I spend some time writing code in XTend. It is nice language, but inferior compared to Scala or Kotlin. There was one nice fieature I would like to see in Kotlin. When passed as annonymous class with single method, lambda is converted to annonymous class.

Checkout lambdas documentation:

Lambdas are expressions which produce Function objects. The type of a lambda expression generally depends on the target type, as seen in the previous examples. That is, the lambda expression can coerce to any interface which has declared only one method (in addition to the ones inherited from Object). This allows for using lambda expressions in many existing Java APIs directly.

Some code examples ( [something] is lambda in XTend):

    // Java Code!
  final JTextField textField = new JTextField();
  textField.addActionListener(new ActionListener() {
  public void actionPerformed(ActionEvent e) {
  textField.setText(“Something happened!”);

  //the same code in XTend
  val textField = new JTextField
  textField.addActionListener([ ActionEvent e |   //lambda start
  textField.text = “Something happened!”
  ]) //lambda end

  //other usage
  val Runnable runnable = [ |
  println(“Hello I’m executed!”)

Similar code in Kotlin would be

    val textField = JTextField()
    textField.addActionListener{  e:ActionEvent ->   //lambda start
        textField.text = "Something happened!"
    } //lambda end

  val runnable:Runnable = {
  println(“Hello I’m executed!”)

I write lot of concurrent & Swing code. Without this feature (in Scala and Kotlin) I have to write tons of wrappers. This feature would be great help.

Also Java8 is comming. I would bet it  will have the same lambda conversion.


See this issue.


Thanks for linking that issue. I think this feature will become more import, when Java8 will be out :-)


Thanks for this link. Those workarounds seems to be usable.