我最近将我的大部分项目转换为kotlin.现在我遇到了一些似乎与注释库有关的异常错误.毋庸置疑,它并没有发生在Java中.
我将描述这些案例 - 一个在Dagger,一个在Butterknife.
1.当具有@Provides相同名称的不同模型中的2 种方法时.例如,在具有"provideFooOrBar"方法的文件"FooProvider.kt"中
@Module
class FooProvider(private val view: FooActivity) {
...
@Provides @FooScope fun provideView() = view
@Provides @FooScope fun provideFooOrBar() = Foo()
}
Run Code Online (Sandbox Code Playgroud)
并使用相同的方法名称另一个文件"BarProvider.kt"
@Module
class BarProvider(private val view: BarActivity) {
...
@Provides @BarScope fun provideView() = view
@Provides @BarScope fun provideFooOrBar() = Bar()
}
Run Code Online (Sandbox Code Playgroud)
在这种情况下,Dagger无法生成一些工厂库,我得到以下编译错误:
Error:(27, 32) error: cannot find symbol class FooProvider_ProvideFooOrBarFactory
可以在https://github.com/maxandron/DaggerIssue325找到重现该问题的示例项目
2.使用Butterknife时这是一个问题.@Bind在两个不同的类中有两个带注释的变量时 - 其中一个只是在运行时无法初始化而没有任何编译错误!
例如,如果我有:
class FooActivity {
@Bind(R.id.foo) lateinit var mFoo: View
}
class NotFooActivity …Run Code Online (Sandbox Code Playgroud) 我已经在我的项目中使用RxJava大约一年了.随着时间的推移,我变得非常喜欢它 - 现在我想的可能太多了......
我现在写的大多数方法都有某种形式的Rx,这很棒!(直到它不是).我现在注意到一些方法需要大量工作来组合不同的可观察生产方法.我觉得虽然我理解我现在写的东西,但下一个程序员将很难理解我的代码.
在我到达底线之前,让我直接从我在Kotlin的代码中给出一个例子(不要深入研究它):
private fun <T : Entity> getCachedEntities(
getManyFunc: () -> Observable<Timestamped<List<T>>>,
getFromNetwork: () -> Observable<ListResult<T>>,
getFunc: (String) -> Observable<Timestamped<T>>,
insertFunc: (T) -> Unit,
updateFunc: (T) -> Unit,
deleteFunc: (String) -> Unit)
= concat(
getManyFunc().filter { isNew(it.timestampMillis) }
.map { ListResult(it.value, "") },
getFromNetwork().doOnNext {
syncWithStorage(it.entities, getFunc, insertFunc, updateFunc, deleteFunc)
}).first()
.onErrorResumeNext { e -> // If a network error occurred, return the cached data and the error
concat(getManyFunc().map { ListResult(it.value, "") }, error(e))
}
Run Code Online (Sandbox Code Playgroud)
简而言之,它的作用是: