我想是這樣的:Scala Mutable Option?
我不是在尋找一個通用的HashMap基於記憶化like this。我試圖用一個var Option[A]
實現它,但它看上去並不十分地道的對我說:
private var cachedResponse: Option[A] = None
def get: A = cachedResponse getOrElse {
cachedResponse = Option(db.findModel())
cachedResponse.get
}
def update: Unit = {
db.updateModel
cachedResponse = None
}
一個快速的谷歌搜索發現這個實現,看起來像你想要的...:http://blog.adamdklein.com/?p=689 – experquisite 2014-10-20 21:59:09