Reputation: 12416
I am trying to implement non-nullable getter with nullable setter and nullable field.
parent
can be null, which means that the parent is this
. If parent is not null, the parent is the parent
value.this
or parent
I tried this:
@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "parent_id")
var _parent: T? = null
var parent: T
get() = if (isParent) this as T else _parent!!
set(value) {
_parent = if (value == null) null else value.parent
}
I don't like the _parent
variable, but also it doesn't help with the setter, because it is still not-nullable as the parent: T
, so the solution does not work.
Upvotes: 1
Views: 2047
Reputation: 97338
At this time it's not possible to define a property with different getter and setter types. There is an open feature request for this functionality, but it's not planned for any specific Kotlin version.
Upvotes: 7