2017-04-18 473 views
2

在Kotlin中獲取泛型類型的實例的最佳方式是什麼?我希望能找到下面的C#代碼的最好的(如果不是100%完美)近似:實例化Kotlin中的泛型類型

public T GetValue<T>() where T : new() { 
    return new T(); 
} 
+3

只需接收'() - > T'作爲您的方法的參數。 –

+2

通常認爲最好的做法是在你使用這樣的代碼的任何地方傳遞一個'() - > T'',並讓調用站點處理實例化,因爲你不能保證'T'將會有一個無參數的構造函數。 –

+2

@YoavSternberg你擊敗了我20秒:) –

回答

2

編輯:正如評論所說,這可能是一個壞主意。接受() -> T可能是實現此目的最合理的方法。也就是說,下面的技巧將會實現你正在尋找的東西,如果不一定是最習慣的方式。不幸的是,你不能直接實現這個目標:Kotlin受其Java系統的阻礙,所以泛型在運行時被刪除,這意味着T不再可以直接使用。使用反射和在線功能,可以解決這個問題,雖然:

/* Convenience wrapper that allows you to call getValue<Type>() instead of of getValue(Type::class) */ 
inline fun <reified T: Any> getValue() : T? = getValue(T::class) 

/* We have no way to guarantee that an empty constructor exists, so must return T? instead of T */ 
fun <T: Any> getValue(clazz: KClass<T>) : T? { 
    clazz.constructors.forEach { con -> 
     if (con.parameters.size == 0) { 
      return con.call() 
     } 
    } 
    return null 
} 

如果再加上一些樣品類,你可以看到,當一個空的構造存在,這將返回一個實例,否則返回null:

class Foo() {} 
class Bar(val label: String) { constructor() : this("bar")} 
class Baz(val label: String) 

fun main(args: Array<String>) { 
    System.out.println("Foo: ${getValue<Foo>()}") // [email protected] 
    // No need to specify the type when it can be inferred 
    val foo : Foo? = getValue() 
    System.out.println("Foo: ${foo}") // [email protected] 
    System.out.println("Bar: ${getValue<Bar>()}") // Prints [email protected] 
    System.out.println("Baz: ${getValue<Baz>()}") // null 
}