【发布时间】:2017-12-31 00:22:10
【问题描述】:
我有一个通用函数,它需要实例化其通用参数的对象并将其传递给某个接口的实例。
据我所知,实例化该通用对象的唯一方法是使函数内联并具体化该类型参数。但我不想公开该接口的实现。
问题是内联函数不能使用内部类。
我基本上想要的是这样的:
/* The interface I want to expose */
interface Params<T> {
val doc: T
}
/* The implementation I do not want to expose */
internal class ParamsImpl<T> (override val doc: T) : Params<T>
/* The function */
inline fun <reified T> doSomething(init: Params<T>.() -> Unit) {
val doc= T::class.java.newInstance()
val params = ParamsImpl(doc) // Can't compile since ParamsImpl is internal
params.init()
}
【问题讨论】: