Syntax for Getters and Setters in Java Classes

Hey!

Are there plans to support the short syntax for getters and setters in Java classes, as it is already supported for Kotlin properties?

Kotlin with JavaFX:

translate.setX(centerPoint.getX() - targetCoordinate.getX()) translate.setY(centerPoint.getY() - targetCoordinate.getY()) scale.setX(targetZoom.getX()) scale.setY(targetZoom.getY()) scale.setPivotX(targetCoordinate.getX()) scale.setPivotY(targetCoordinate.getY())

Groovy with JavaFX:

translate.x = centerPoint.x - targetCoordinate.x translate.y = centerPoint.y - targetCoordinate.y scale.x = targetZoom.x scale.y = targetZoom.y scale.pivotX = targetCoordinate.x scale.pivotY = targetCoordinate.y

I started to convert some Groovy code to Kotlin and converting the getters and setters to old Java syntax was tedious. Another point was changing code with integers to doubles, since Kotlin adheres strongly to the method's argument types; which I find ok, if this prevents centain sources for coding errors.

–Benjamin

It is done: https://youtrack.jetbrains.com/issue/KT-400 (will be introduced in M13)

Wow! Very good.

I’ve updated to the Kotlin IDEA plugin 0.13.343. The property access works great.

Small issues:

(1) The accessed Java field (property) is underlined, but is missing a tooltip that explains why. Like the tooltip that explains that a value is captured within a closure.

(2) Calling a Java setter via property access syntax does not work, when the corresponding Java getter is missing.

Also consider this:

fun main(args: Array<String>) {
    val stage = javafx.stage.Stage()

  // works:
  stage.setOnShown {
  println(“stage.onShown”)
  }

  // error: Type mismatch.
  stage.onShown = {
  println(“stage.onShown”)
  }
}

> (1) The accessed Java field (property) is underlined, but is missing a tooltip that explains why. Like the tooltip that explains that a value is captured within a closure.

What do you mean? Could you explain it on some example, please?

> (2) Calling a Java setter via property access syntax does not work, when the corresponding Java getter is missing.

Currently, Kotlin does not support properties that have no getter (write-only properties). That’s why we cannot use property syntax when only set-method exist. Maybe this will be changed later (after 1.0 release).

hastebrot wrote:

Also consider this:

fun main(args: Array<String>) {
    val stage = javafx.stage.Stage()

  // works:
  stage.setOnShown {
  println(“stage.onShown”)
  }

  // error: Type mismatch.
  stage.onShown = {
  println(“stage.onShown”)
  }
}



Unfortunately, the example with “onShown” assignment of a lambda cannot work because there is no conversion between SAM-interfaces in Java and functional types in Kotlin. The reason why you may have an impression that there is one, is that for every java function accepting a SAM-interface parameter we generate a synthetic extension function accepting functional type. That’s how it works for the call of “setOnShown”. But we cannot implement it for property because than we should have 2 different “onShown” properties - one of SAM-type and another of functional type.
I can suggest to use the following syntax:

stage.onShown = EventHandler { println(...) }