How to declare raw types in Kotlin?

In Java, I can declare this

private List a; //onCreate a = new ArrayList() 

But in Kotlin, he shows an error, he forces me to set the type

 private List<String> a 

Sometimes I donโ€™t want to provide a type (I donโ€™t need it), but it shows an error in Kotlin For example In Java

 public abstract class BaseActivity<T extends ViewDataBinding> extends AppCompatActivity { //something } public abstract class BaseFragment { private BaseActivity activity; //something } //in kotkin I can't write lateinit var activity: BaseAtivity //show error here (I have to specific a type but this is the base class and I do not want to specific a type here). I just want a reference of BaseActivity @Override public void onAttach(Context context) { super.onAttach(context); if (context instanceof BaseActivity) { //good in java but show error //in kotlin because I have to //specific a type like BaseAtivity<something> BaseActivity activity = (BaseActivity) context; this.mActivity = activity; activity.onFragmentAttached(); } } 

What can I write in Kotlin to get the same code in java

+5
source share
2 answers

You can use star predictions

eg.

 private List<*> a 
+4
source

Quoted from Docs : "Java kernel types are converted to star projections, List becomes List<*> !, I.e. List<out Any?> !."

This way you can use stellar projection like <*> .

+1
source

Source: https://habr.com/ru/post/1272745/


All Articles