2
class TaskRepo(taskData: TaskData) {

companion object {
    private val repoByTask: LRUMap<String, OrderFormRepo> = LRUMap(2, 10);

     fun getInstance(taskData: TaskData): OrderFormRepo {
        if (notFoundObject(taskData.taskId)) {
            repoByTask[taskData.taskId] = OrderFormRepo(taskData);
        }
        return repoByTask[taskData.taskId];//PROBLEM HERE
    }

    private fun notFoundObject(taskId: String): Boolean {
        if (repoByTask.containsKey(taskId) && repoByTask[taskId] != null) {
            return false
        }
        return true
    }
}

}

in getInstance method of companion object I am getting compile time error: Required TaskRepo and found TaskRepo?


Spyder doesn't detect changes in imported python files

I'm using Spyder 3.2.4 (Python 3.6). Spyder doesn't detect changes in imported python files. For example:

test2.py:

def func():
    return 5

test1.py:

import test2

a = test2.func()
print(a)

When I wrote those classes, and saved them (in the same working directory), and ran test1.py the output was 5.

BUT when I change the function in test2.py, to like:

def func():
    return 10

Save it, and then run python1.py, I still get 5. Only when I save, exit the IDE, and return, I will get the changed code (10).

This behavior is going on since I started using Spyder (few months by now), and it's super annoying. Help would be appreciated.

4

1 回答 1

4

LRUMap实现Map接口,该get方法在 Kotlin 中返回 a V?,因为它null在给定键不存在任何元素时返回。

由于您已经在这种情况下事先进行了检查,因此您可以合理确定(假设没有其他线程同时修改映射)该值不会为空,并强制转换为不可为空的使用!!运算符键入:

return repoByTask[taskData.taskId]!!

有关从 a 读取时处理丢失键的其他方法Map,请参阅getOrDefaultgetOrElse方法。

于 2017-12-09T17:09:49.810 回答