【问题标题】:How to instantiate a new instance of generic type如何实例化泛型类型的新实例
【发布时间】:2015-10-21 22:41:30
【问题描述】:

在 C# 中,您可以在泛型上添加新约束以创建泛型参数类型的新实例,在 Kotlin 中是否有等效项?

现在我的工作是这样的:

fun <T> someMethod(class : () -> T) {
    val newInstance = class()
}

我像这样调用 someMethod()

someMethod<MyClass>(::MyClass)

但我想做这样的事情:

fun <T : new> someMethod() {
    val newInstance = T()
}

这可能吗?

【问题讨论】:

标签: kotlin


【解决方案1】:

目前,这是不可能的。您可以为问题点赞https://youtrack.jetbrains.com/issue/KT-6728 投票支持添加此功能。

至少,您可以省略泛型类型,因为 Kotlin 可以推断它:

someMethod(::MyClass)

【讨论】:

    【解决方案2】:

    解决方案:

    1/ 使用保留参数类型(具体类型)的内联函数

    2/ 在这个内联函数中,使用类自省(反射 *)调用所需的构造函数 /!\ 内联函数不能嵌套/嵌入到类或函数中

    让我们通过一个简单的例子来看看它是如何工作的:

    // Here's 2 classes that take one init with one parameter named "param" of type String
    //!\ to not put in a class or function
    
    class A(val param: String) {}
    class B(val param: String) {}
    
    // Here's the inline function.
    // It returns an optional because it could be passed some types that do not own
    // a constructor with a param named param of type String
    
    inline fun <reified T> createAnInstance(value: String) : T? {
    
        val paramType = String::class.createType() //<< get createAnInstance param 'value' type
    
        val constructor = T::class.constructors.filter {
            it.parameters.size == 1 && it.parameters.filter { //< filter constructors with 1 param
                it.name == "param" && it.type == paramType //< filter constructors whose name is "param" && type is 'value' type
            }.size != 0
        }.firstOrNull() //< get first item or returned list or null
    
        return constructor?.call(value) // instantiate the class with value
    
    }
    
    // Execute. Note that to path the type to the function val/var must be type specified. 
    
    val a: A? = createAnInstance("Wow! A new instance of A")
    
    val b: B? = createAnInstance("Wow! A new instance of B")
    

    *) kotlin-reflect.jar 必须包含在项目中

    在 Android Studio 中:添加到 build.gradle(Module: app): implementation "org.jetbrains.kotlin:kotlin-reflect:$kotlin_version"

    【讨论】:

      猜你喜欢
      • 2013-10-03
      • 2011-04-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-19
      相关资源
      最近更新 更多