Can I define “method-private” fields in Scala?
问题 Given this situation: object ResourceManager { private var inited = false def init(config: Config) { if (inited) throw new IllegalStateException // do initialization inited = true } } Is there any way that I could make inited somehow “private to init()”, such that I can be sure that no other method in this class will ever be able to set inited = false ? 回答1: Taken from In Scala, how would you declare static data inside a function?. Don’t use a method but a function object: val init = { // or