{"action":"created","issue":{"url":"https://api.github.com/repos/edvin/tornadofx/issues/3","repository_url":"https://api.github.com/repos/edvin/tornadofx","labels_url":"https://api.github.com/repos/edvin/tornadofx/issues/3/labels{/name}","comments_url":"https://api.github.com/repos/edvin/tornadofx/issues/3/comments","events_url":"https://api.github.com/repos/edvin/tornadofx/issues/3/events","html_url":"https://github.com/edvin/tornadofx/issues/3","id":133217146,"number":3,"title":"Delegates for JavaFX Properties","user":{"login":"hastebrot","id":496255,"avatar_url":"https://avatars.githubusercontent.com/u/496255?v=3","gravatar_id":"","url":"https://api.github.com/users/hastebrot","html_url":"https://github.com/hastebrot","followers_url":"https://api.github.com/users/hastebrot/followers","following_url":"https://api.github.com/users/hastebrot/following{/other_user}","gists_url":"https://api.github.com/users/hastebrot/gists{/gist_id}","starred_url":"https://api.github.com/users/hastebrot/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/hastebrot/subscriptions","organizations_url":"https://api.github.com/users/hastebrot/orgs","repos_url":"https://api.github.com/users/hastebrot/repos","events_url":"https://api.github.com/users/hastebrot/events{/privacy}","received_events_url":"https://api.github.com/users/hastebrot/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":8,"created_at":"2016-02-12T11:28:29Z","updated_at":"2016-02-13T13:17:39Z","closed_at":null,"body":"I wonder if we could simplify the creation of `Property` fields and related methods somehow. In Java the general structure of a read-write property is (raw version):\r\n\r\n~~~java\r\nprivate Property<T> fooProperty;\r\npublic Property<T> fooProperty() { return fooProperty; }\r\npublic void setFoo(T value) { /* lazy create property object and set value */ }\r\npublic T getFoo() { /* lazy create property object and get value */ }\r\n~~~\r\n\r\nThis could be expressed in Kotlin as:\r\n\r\n~~~kotlin\r\nprivate val fooProperty by lazy { SimpleObjectProperty<T>() }\r\nfun fooProperty() = fooProperty\r\nvar foo: T\r\n    get() = fooProperty.get()\r\n    set(value) = fooProperty.set(value)\r\n~~~\r\n\r\nIn `#javafx` on kotlinlang.slack.com I wrote an incomplete implementation using Kotlin's delegated properties to simply this code. It could look like this:\r\n\r\n~~~kotlin\r\nvar foo by lazyProperty { SimpleObjectProperty<T>() }\r\nfun fooProperty() = foo.internalProperty\r\n~~~\r\n\r\n(This is an excellent framework. Good work.)\r\n\r\n\r\n"},"comment":{"url":"https://api.github.com/repos/edvin/tornadofx/issues/comments/183662812","html_url":"https://github.com/edvin/tornadofx/issues/3#issuecomment-183662812","issue_url":"https://api.github.com/repos/edvin/tornadofx/issues/3","id":183662812,"user":{"login":"hastebrot","id":496255,"avatar_url":"https://avatars.githubusercontent.com/u/496255?v=3","gravatar_id":"","url":"https://api.github.com/users/hastebrot","html_url":"https://github.com/hastebrot","followers_url":"https://api.github.com/users/hastebrot/followers","following_url":"https://api.github.com/users/hastebrot/following{/other_user}","gists_url":"https://api.github.com/users/hastebrot/gists{/gist_id}","starred_url":"https://api.github.com/users/hastebrot/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/hastebrot/subscriptions","organizations_url":"https://api.github.com/users/hastebrot/orgs","repos_url":"https://api.github.com/users/hastebrot/repos","events_url":"https://api.github.com/users/hastebrot/events{/privacy}","received_events_url":"https://api.github.com/users/hastebrot/received_events","type":"User","site_admin":false},"created_at":"2016-02-13T13:17:39Z","updated_at":"2016-02-13T13:17:39Z","body":">I haven't quite grasped why the lazy field would loose its laziness\r\n\r\n~~~kotlin\r\n@Test\r\nfun `property() in combination with lazy()`() {\r\n    val fixture = object {\r\n        private val stringProperty by lazy {\r\n            println(\"in lazy lambda expr\")\r\n            SimpleObjectProperty<String>()\r\n        }\r\n\r\n        val string by property(stringProperty)\r\n    }\r\n}\r\n~~~\r\n\r\nUsing the reference to `stringProperty` for the `string` field, will immediately call the lazy lambda. We can see this using the `print()` statement. Unfortunately the Kotlin compiler doesn't allow variables from the outside in `lazy {}`, to enable us to automatically test this behaviour.\r\n\r\n(Note: I've added `property(Property<T>)` into `Properties.kt`.)\r\n\r\nAlex Hart from `#javafx` proposed adding a `property(Lazy)` method. I'll need to investigate this further.\r\n\r\n>You can preserve laziness by implementing a ReadOnlyPropertyDelegate which takes a Lazy<T> as an argument, and delegates this to a val within your delegate class, for example.  (Or, alternatively, a LazyPropertyDelegate which takes a codeblock, and passes that to lazy())"}}