https://kotlinlang.org logo
#getting-started
Title
# getting-started
m

martmists

05/04/2022, 11:33 PM
When working with delegates, how should I handle situations like this?
Copy code
class MonoNode : BaseNode() {
    protected val input by input()    // getValue should resolve here; it's a delegate to use the property name
    protected val output by output()  // getValue should not resolve as its value may change over time

    override fun process() {
        // We don't want to call getValue too often in real-time code that needs to be fast!
        // output.getValue should only be called once per process() call
        // input.getValue should only be called in constructor/init
        // Is there a good way to do it without having to store to a variable?

        val x = input[0]   // calls input.getValue
        val y = input[1]   // calls input.getValue
        output[0] = x + y  // calls output.getValue
        output[0] = x + y  // calls output.getValue
    }
}
🚫 1
c

Chris Lee

05/05/2022, 12:26 AM
could be partly solved with the use of
lazy
to memoize values, such that the ‘expensive’ portion only occurs on the first call.
2 Views