Repository navigation
lateinit val
#475
Replies: 7 comments 16 replies
I don't understand the point of this. What's the use of the stability contract if it doesn't end up affecting smartcasts? The only situation I can see is someone explicitly using I see no value in subclassing |
|
I like the proposal, but as user, the difference between When I migrate my
Especially for kotlinx-serialization, I do see a use-case for readOnly values used in JSON like database ids. As I user, I would expect the {"id": 42, "role": "user"}@Entity
data class User(
@Column
var name : String = ""
) {
@Id
@GeneratedValue
lateinit val id : Long
}
val newUser = User().apply { name = "John") }
save(newUser)
val id = newUser.id // 42
val dtoUser = User("John")
val receivedUser = sendToServer(Json.encodeAsString(dtoUser)) // {"role": "user"}
receivedUser == "{"id": 42, "role": "user"}" |
|
What's the visibility of the "setter"? Will the assignment only be private? @Serializable
public data class User(
val name : String
) {
public lateinit val databaseId : Long
}
val user = User("John")
val id = user.id // will throw
user.id = 42 // accessible or not? |
|
Wouldn't it be possible to compile Basically, the compilation scheme for class Example {
@Volatile
private var _property: Any? = UNINITIALIZED
fun getProperty(): T {
val p = _property
if (p === UNINITIALIZED) throw TODO()
return (p as T)
}
fun setProperty(v: T) {
val updated = propUpdater.compareAndSet(this, UNINITIALIZED, v)
if (!updated) throw TODO()
}
private companion object {
private val propUpdater = AtomicReferenceFieldUpdater.newUpdater(
Example::class.java, Any::class.java, "_property"
)
}
}And on Native. instead of AFU/VarHandle APIs, I believe it is possible to use the same intrinsics that Atomics are using internally. So given KEEP's arguments for delegate:
with the field-based compilation scheme:
|
|
After internal discussions, taking into account your feedback, we decided to:
The KEEP is updated accordingly. We welcome your feedback. |
This looks very weird and definitely it isn't good UX from a DI user perspective. You'll get a lot of "???" moments here. Note that DI users now must use |
|
Hello! During an extended internal discussion, we reassessed our compilation strategy for
For the experimental release, we have therefore decided not to make Consequently, multiple unsynchronized writes from different threads may violate the assign-once guarantee. Because We welcome user feedback and may consider the following improvements:
See the updated KEEP for details. |
Uh oh!
There was an error while loading. Please reload this page.
Discussion for the
lateinit valproposal. The full text is available here.Note: This proposal supersedes KEEP-0452 and its discussion, capturing the resulting design decisions.
We propose to support assign-once properties in Kotlin by introducing the
AssignOnceproperty delegate andlateinit valdeclarations.Both enforce assign-once semantics at runtime: once initialized, a property throws on reassignment.
AssignOnceis a delegate interface in the standard library:@SubclassOptInRequiredopt-in.lateinit valis a declaration that bridges the gap betweenlateinit varandval:AssignOnceimplementation.All reactions