if I have a var lateinit, can I override the gette...
# announcements
x
if I have a var lateinit, can I override the getter to provide a default later? not sure where exactly the non null check in the lateinit will be performed, it seems to be on access
c
lateinit is useful when another class is initializing fields in yours, such as deserialization or DI
x
I didn’t mean that… but can I make it lazy loading in a sub class?
like base class it isn’t subclass it is?
intellij isn’t loving my attempt at lazy
d
Last value in a lambda is the return value of the lambda:
Copy code
by lazy {
    UUID.randomUUID().toString()
}
And a lazy value cannot be
var
x
so then that doesn’t help with what I’m trying to do
d
So you want a
lateinit
, but instead of throwing return a default value?
Does it have to be
lateinit
, as in, do you need the "public field from a Java perspective" that
lateinit
provides?
x
well I have 2 classes, Builder, and TestBuilder, builder should throw if it’s not defined, where TestBuilder should return a default value if it’s not defined
I should say “not defined before get is called” having them both implement Supplier
d
I would just go for a var with custom getter and setter then
Example:
Copy code
open class Builder {
  private var thing0: String? = null
  var thing: String
    get() = thing0 ?: defaultValue()?.also { thing0 = it }
    set(value) {
      thing0 = value
    }

  open fun defaultValue(): String = throw new IllegalStateException()
}

class TestBuilder : Builder() {
  fun defaultValue(): String = "hello"
}
x
ok
Copy code
open class Builder : Supplier<Foley> {
        protected open lateinit var rfid: String
        protected open lateinit var mrn: String
        protected open lateinit var site: String
        protected open var weight: Double? = null
        protected open lateinit var dateFirstUse: LocalDateTime
        protected open lateinit var dateExpiration: LocalDateTime
        protected open lateinit var clinicalStudy: String
        protected open lateinit var lotNumber: String
        protected open lateinit var foleyType: String
        protected open lateinit var timeZone: String

        fun rfid(rfid: String) = apply {
            this.rfid = rfid
        }

        fun mrn(mrn: String) = apply {
            this.mrn = mrn
        }

        fun site(site: String) = apply {
            <http://this.site|this.site> = site
        }

        override fun get(): Foley {
            return Foley(
                rfid = rfid,
                mrn = mrn,
                site = site,
                weight = weight!!,
                dateFirstUse = dateFirstUse,
                dateExpiration = dateExpiration,
                clinicalStudy = clinicalStudy,
                lotNumber = lotNumber,
                foleyType = foleyType,
                timeZone = timeZone
            )
        }

        class TestBuilder : Builder() {
            override var rfid: String = UUID.randomUUID().toString()
            override var mrn: String = UUID.randomUUID().toString()
            override var site: String = UUID.randomUUID().toString()
            override var weight: Double? = 123.0
            override var dateFirstUse: LocalDateTime = LocalDateTime.now()
            override var dateExpiration: LocalDateTime = LocalDateTime.now().plusDays(1)
            override var clinicalStudy: String = UUID.randomUUID().toString()
            override var lotNumber: String = "n/a"
            override var foleyType: String = "n/a"
            override var timeZone: String = "PST"
        }
    }
seems to work well enough, now just to add the rest of the setters/behavior