Closed LouisCAD closed 7 years ago
I'm guessing some issue with the Kotlin generics compatibility, so reporting there was def. the right approach @maxtroy I assume there's not much we can do with the use of generics here, fixing in Kotlin is the only way forward?
We can't really do anything, I'm afraid.
Because Android Studio is the recommended way to program Android code, the current design places Android Studio use case first, making sure the IDE sees the right types and auto-completes the right subset of methods available at any given state.
The generics we use in the repository compiler are a bit quirky (and towards the verbose/redundant end of the spectrum), due to Android Studio (or IntelliJ) internal type checker having some difference with the real Java compiler. In particular, Android Studio does NOT like the following class hierarchy:
interface Base<T> {}
interface X<T> extends Base<X<T>> {}
interface Y<T> extends Base<Y<T>> {}
@SuppressWarnings({"rawtypes", "unchecked"})
interface Compiler extends X, Y {}
With the error that "Base<T>
cannot be extended with different type parameters", despite the explicit request to ignore the type parameters (suppressing raw types and unchecked casts warnings).
Note that this is IntelliJ-only; the real Java compiler has no issues with this hierarchy. This bug has been reported more than one year ago, with no official solution so far. The way we work around this IntelliJ limitation is to allow the "different" type parameters to unify:
interface Base<T> {}
interface X<T, S extends X<T, S>> extends Base<S> {}
interface Y<T, S extends Y<T, S>> extends Base<S> {}
@SuppressWarnings({"rawtypes", "unchecked"})
interface Compiler extends X, Y {}
Because in this way, the ("unnecessarily smart") IntelliJ type inference system can find a common subclass S that extends both X<T, S>
and Y<T, S>
(aka Compiler), so the interface Base<S>
is inherited with the same type parameter.
I'm guessing that the Kotlin compiler might not be able to handle this type hierarchy and gives up at some point.
Closing this, since there's not much that can be done in Agera. Kotlin promises Java interop. so hopefully KT-14725 can be resolve soon.
fun <F : RepositoryCompilerStates.RFlow<*, *, *>, T> F.asT(c: Class<T>? = null): RepositoryCompilerStates.RFlow<T, T, *> = this as RepositoryCompilerStates.RFlow<T, T, *>
fun <F : RepositoryCompilerStates.RFlow<*, *, *>, T> F.asResultT(c: Class<T>? = null): RepositoryCompilerStates.RFlow<Result<T>, T, *> = this as RepositoryCompilerStates.RFlow<Result<T>, T, *>
@TestOnly
fun test(): Repository<Result<String>> {
return Repositories.repositoryWithInitialValue(Result.absent<String>())
.observe()
.onUpdatesPerLoop()
.goTo(Executors.threadPollExecutor())
.asT(Result::class.java)
.attemptGetFrom { Result.present("123") }
.orEnd { Result.failure<String>() }
.check { it.isNotEmpty() }
.orEnd { Result.failure() }
.asResultT(String::class.java)
.sendTo { Log.e("TTEST", "sendTo") }
.asResultT(String::class.java)
.transform { "111" }
.thenTransform { Result.present(it) }
.notifyIf { _, _ -> true }
.onConcurrentUpdate(RepositoryConfig.SEND_INTERRUPT)
.compile()
}
@sfcecy7i Should work with reified
too.
inline fun <reified F : RepositoryCompilerStates.RFlow<*, *, *>, T> F.typed(): RepositoryCompilerStates.RFlow<T, T, *> = this as RepositoryCompilerStates.RFlow<T, T, *>
inline fun <reified F : RepositoryCompilerStates.RFlow<*, *, *>, T> F.typedResult(): RepositoryCompilerStates.RFlow<Result<T>, T, *> = this as RepositoryCompilerStates.RFlow<Result<T>, T, *>
@TestOnly
fun test(): Repository<Result<String>> {
return Repositories.repositoryWithInitialValue(Result.absent<String>())
.observe()
.onUpdatesPerLoop()
.goTo(Executors.threadPollExecutor())
.typed<Result>()
.attemptGetFrom { Result.present("123") }
.orEnd { Result.failure<String>() }
.check { it.isNotEmpty() }
.orEnd { Result.failure() }
.typedResult<String>()
.sendTo { Log.e("TTEST", "sendTo") }
.typedResult<String>()
.transform { "111" }
.thenTransform { Result.present(it) }
.notifyIf { _, _ -> true }
.onConcurrentUpdate(RepositoryConfig.SEND_INTERRUPT)
.compile()
}
@LouisCAD Thanks for your advice.
inline fun <reified TPre, reified TVal> RepositoryCompilerStates.RFlow<*, *, *>.reify() = this as RepositoryCompilerStates.RFlow<TVal, TPre, *>
inline fun <reified TVal> RepositoryCompilerStates.RFlow<*, *, *>.reify1() = this as RepositoryCompilerStates.RFlow<TVal, *, *>
@TestOnly
fun test(): Repository<Result<String>> {
return Repositories.repositoryWithInitialValue(Result.absent<String>())
.observe()
.onUpdatesPerLoop()
.goTo(KExecutors.threadPollExecutor())
.reify1<Result<String>>()
.attemptGetFrom { Result.present("success") }
.orEnd { Result.failure<String>() }
.check { it.isNotEmpty() }
.orEnd { Result.failure() }
.sendTo { Log.e("TAG", "result: $it") }
.reify<String, Result<String>>()
.transform { "$it end" }
.thenTransform { Result.present(it) }
.notifyIf { _, _ -> true }
.onConcurrentUpdate(RepositoryConfig.SEND_INTERRUPT)
.compile()
}
EDIT: Here's the kotlin issue in Youtrack Hi, I tried using complied repositories in kotlin, first manually, by writing the code myself in a new kotlin class, but with no success. I then tried to convert the
CalculatorActivityFinal.java
file to to kotlin with the IDE option (cmd+option+shift+K) (requires the IntelliJ/Android Studio Kotlin plugin), and the generated code couldn't compile either, displaying the same error. This seems to be related to how kotlin handles generics type safety.Here's what I see after converting code to kotlin (all the errors are in
onStart()
): Note that the code above was generated after converting all lambdas to anonymous classes. The main problematic line is the repository compilation:Before trying with anonymous classes, I tried converting the codes with lambdas, which did not work too as you can see in the following screenshot:
I think it's absolutely impossible to compile an agera repository in kotlin with agera 1.1.0 and kotlin 1.0.5, so I'm asking how agera, or kotlin may be edited to support such cases