Is there some syntax sugar for read-only property caching?

Solved.

Let’s say that a read-only property is actually the result of a long operation. Repeating the same operation again and again is an unnecessary time-waste. I can cache it like below, but it seems like bloated, especially if there are many such properties. I wonder if there is some sort syntax sugar that I do not know of to make this simpler.

class Dog { private var nameCache:String?=null; val name:String get() { if(nameCache == null) { nameCache = calculateName(); } return nameCache!!; } private fun calculateName():String { return "some" + "complicated" +"function"; } } 

submitted by /u/evolution2015
[link] [comments]