https://kotlinlang.org logo
#getting-started
Title
# getting-started
v

ValV

09/08/2018, 10:53 PM
If I use properties in javafx, for each I should use 3 methods:
Copy code
class MyClass {
  private StringProperty myObservable = new SimpleStringProperty()
  public final String getMyObservable() { return myObservable.get(); }
  public final void setMyObservable (String value) = { myObservable.set(value); }
  public StringProperty myObservableProperty() { return myObservable; }
}
But doing something like:
Copy code
val myClass = MyClass()
val property = myClass.myObservableProperty()
myClass.setMyObservable("New data")
println(myClass.getMyObservable())
is not "true" Kotlin way, that should look something like:
Copy code
val myClass = MyClass()
val property = myClass.myObservableProperty() // How to rewrite this line?
myClass.myObservable = "New data"
println(myClass.myObservable)
since rewritten Kotlin variant looks like:
Copy code
class MyClass {
  private var _myObservable: StringProperty = SimpleStringProperty()
  var myObservable: String
    get() = _myObservable.get()
    set(value) = _myObservable.set(value)

  fun myObservableProperty() = _myObservable // Is there a way to fold it like getters and setters?
}
Are there some tricks to hold properties in a better way?