Smart cast 'Loop' is impossible, because 'loop' is a property that has open or custom getter

abstract class Envelope {

    abstract val propertyType: PropertyType
    abstract val envelopeExpression: String
    abstract val loop: Loop?

    /**
     * Returns the envelope's value for a certain frame t.
     * @param t The frame index. If loop is not set to null, the loop index will be calculated and used instead of t.
     */
    fun getValueAt(t: Int): Double {
        var index = t
        if (loop != null)
            index = 5 % loop.duration

        return ExpressionBuilder(envelopeExpression)
            .variables("t")
            .build()
            .setVariable("t", index.toDouble()).evaluate()
    }
}

Why does it not let me access the loop variable?
“Smart cast ‘Loop’ is impossible, because ‘loop’ is a property that has open or custom getter”
Thank you!

This is mentioned in the documentations:

The important parts for your scenario are:

smart casts work only when the compiler can guarantee that the variable won’t change between the check and the usage.

And

Smart casts cannot be used on open properties or properties that have custom getters.

Your property is open because it is abstract.

What it means is that a derived class could define a custom getter that returns a non-null value when you do the null check and then a null value when you access the property again.

loop?.let { … } could help you here.

3 Likes