Can't use custom getter with delegated property

can synonym
can meaning
can noun meaning
can.'' acronym
cant
could
meaning of can in hindi
cane

I am new to kotlin and can't figure out this issue.

I have a property that is not nullable and may or may not be used. Hence I have delegated it in a lazy way so that it is initialized when required.

private val labelAnimator: ObjectAnimator by lazy {
    ObjectAnimator.ofFloat(this, "floatingLabelFraction", 0f, 1f)
}

However, I also need to set some property of that object every time it is accessed.

fun getLabelAnimator(): ObjectAnimator {
    labelAnimator.duration = (if (isFloatingLabelAnimating) 300 else 0).toLong()
    return labelAnimator
}

But I cannot use a custom getter since the property is delegated. How do I achieve this in best possible way?

You could for example use your original property as Backing Property and actually define the property as a delegation to it.

private val _labelAnimator by lazy {
    ObjectAnimator.ofFloat(this, "floatingLabelFraction", 0f, 1f)
}

val labelAnimator: ObjectAnimator
    get() {
        _labelAnimator.duration = if (isFloatingLabelAnimating) 300L else 0L
        return _labelAnimator
    }

This gives you a getLabelAnimator() method in Java and direct property access in Kotlin.

But like it was stated several times before, it might not be the best way to implement such behavior. A simple factory could do the same with little overhead.

Can, 1.4Used to indicate that something is typically the case. 'antique clocks can seem out of place in modern homes'. More example sentences. Can definition, to be able to; have the ability, power, or skill to: She can solve the problem easily, I'm sure. See more.

I am not a Kotlin expert, but it sounds wrong to me. By delegating to lazy you want to init object on its first getter call. And then, you are trying to write custom getter. I see a conflict there.

Maybe:

private val labelAnimator: ObjectAnimator by lazy {
    val labelAnimator = ObjectAnimator.ofFloat(this, "floatingLabelFraction", 0f, 1f)
    labelAnimator.duration = (if (isFloatingLabelAnimating) 300 else 0).toLong()
    labelAnimator
}

CAN, Can may refer to: Contents. 1 Containers; 2 Music; 3 Other; 4 See also. Containers[edit]. Aluminum can · Drink can · Oil can · Steel and tin cans · Trash can  Define can. can synonyms, can pronunciation, can translation, English dictionary definition of can. to be able to, have the power or skill to: I can take a bus to the

The shortest version

private val labelAnimator: ObjectAnimator by lazy {
    ObjectAnimator.ofFloat(this, "floatingLabelFraction", 0f, 1f).apply {
        duration = if (isFloatingLabelAnimating) 300L else 0L
    }
}

Can, can meaning, definition, what is can: to be able to do something or to know ho: Learn more. 110 synonyms of can from the Merriam-Webster Thesaurus, plus 97 related words, definitions, and antonyms. Find another word for can. Can: to bring (as an action or operation) to an immediate end.

Can, A can is a metal container in which something such as food, drink, or paint is put. The container is usually sealed to keep the contents fresh. Several young men  A Controller Area Network (CAN bus) is a robust vehicle bus standard designed to allow microcontrollers and devices to communicate with each other's applications without a host computer. It is a message-based protocol , designed originally for multiplex electrical wiring within automobiles to save on copper, but can also be used in many other

can, Nas' official music video for 'I Can'. Click to listen to Nas on Spotify: http://smarturl Duration: 4:27 Posted: 22 Mar 2011 Can(stylised as CAN) was a German experimental rockband formed in Colognein 1968 by the core quartet of Holger Czukay(bass, tape editing), Irmin Schmidt(keyboards), Michael Karoli(guitar), and Jaki Liebezeit(drums).

Can definition and meaning, VerbEdit. can (third-person singular simple present can, present participle -, simple past could, past participle (obsolete except in adjectival use) couth). We would like to show you a description here but the site won’t allow us.

Comments
  • While this does not answer your question, I would strongly advise against mutating a property in a getter. It violates the principle of least surprise.
  • Wouldn't it create a new object every time I access the property?