0

在探索 Kodein 时,我遵循https://proandroiddev.com/dependency-injection-with-kotlin-kodein-koin-3d783745e48d中的示例

val diModel = Kodein.Module {
    bind<TasksRepository>() with singleton {
        FakeTasksRepository()
    }
}

我得到了错误

Type inference failed. Expected type mismatched:
required: KodeinBinding<in ???, in ???, out TaskRepository>
found:    (Mutable)set<(() -> FakeTaskRepository)!>!

怎么了?

仅供参考,我使用 kodeIn 版本6.4.0

补充一下,我已经尝试过来自https://kodein.org/di/的示例

val businessModule = Kodein.Module {
    bind<IService>() with singleton { BusinessService() }
}

同样的事情也会发生。我做错什么了吗?或者文档已经过时了?

4

2 回答 2

1

这是一个完整的工作示例:

import org.kodein.di.Kodein
import org.kodein.di.generic.bind
import org.kodein.di.generic.instance
import org.kodein.di.generic.singleton

interface TasksRepository {
    fun log(): String = "EMPTY"
}
class FakeTasksRepository : TasksRepository {
    override fun log(): String = "Hello Kodeiners!"
}

val diModel = Kodein.Module("diModel") {
    bind<TasksRepository>() with singleton {
        FakeTasksRepository()
    }
}

fun main() {
    val kodein = Kodein {
        import(diModel)
    }

    val tasksRepository: TasksRepository by kodein.instance()
    println(tasksRepository.log())
}
于 2019-09-24T14:33:54.227 回答
0

发现问题。我不小心包括

import java.util.Collections.singleton

代替

import org.kodein.di.generic.singleton

于 2019-09-24T12:46:57.763 回答