Colton Idle
05/10/2024, 6:50 PMclass Person(
val nameFirst: String,
uniqueId_: String,
) {
val uniqueId = convertToByteArray(uniqueId_)
questions?
1. this class actually has a bunch more vals in the constructor and it was weird to me seeing uniqueId_ without val
keyword
2. Seems like uniqueId is still a field right, so is there a way this can somehow just become a property with a getter that'll convert the string into byteArray
3. the name of uniqueId_ is odd, no?Landry Norris
05/10/2024, 6:58 PMMichael Krussel
05/10/2024, 6:59 PM_
prefix for backing fields whose name conflicts with the public properties. Common example is a mutable list private and a read only list public property. I've not seen a _
postfix. It would have been possible to declare the two with the same name. But since the type is different, it is a bit confusing. I would be surprised to call a constructor with a string parameter and then have a byte array property from it, with them both having the same name. So I would probably rename one of them to highlight the difference.Youssef Shoaib [MOD]
05/10/2024, 7:40 PMuniqueId_
is not a field. It won't exist in the bytecode as a private field at all. In fact, it's just a constructor parameter. nameFirst
is both a constructor parameter, a field, and a getter, while uniqueId_
is a constructor parameter, and uniqueId
is a field and a getter. I'd say the idiomatic way to do this would actually be:
class Person(
val nameFirst: String,
uniqueId: String,
) {
val uniqueId = convertToByteArray(uniqueId)
because it's perfectly fine to shadow a constructor parameter with a property (arguably, that's what val nameFirst
does anyways)ephemient
05/10/2024, 8:17 PMclass Person(
val name: String,
val uniqueId: ByteArray,
) {
constructor(
name: String,
uniqueId: String,
) : this(
name = name,
uniqueId = uniqueId.toByteArray(),
)
}
Ruckus
05/10/2024, 8:25 PMconvertToByteArray
is doing, so it's not safe to replace it with toByteArray
.)ephemient
05/10/2024, 9:54 PMprivate constructor
or convertToByteArray
or whatever, I just didn't want to have to type it outJacob
05/10/2024, 11:09 PMKlitos Kyriacou
05/13/2024, 8:36 AM