我想调用服务器,但当且仅当数据(在这种情况下为 remoteIdKey)已过时。这是我的做法:
object remoteCaller {
def getRemoteKeyId = {
// do we need to get the data from the server?
if (currentTime - lastCallTime >= 1000) {
remoteId = makeRemoteCall
lastCallTime = currentTime
}
remoteId
}
private var remoteId = 0
private def currentTime = //....
private var lastCallTime = //....
private def makeRemoteCall = { //remote call to the server to get the remote Id key}
}
问题是我改变了 and 的状态remoteCaller
,因此使用var
而不是val
. 而且我还想使用对象而不是类来确保它只是它的实例。
如何使 remoteCaller 不可变或如何以功能样式(没有突变)?