Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Kotlin generics

How is it possible to enforce a generic type for a method in Kotlin? I know for instance you can do the following:

var someVar: MutableSet<out SomeType> = hashSetOf()

How can you do the same for a method?

fun <T> doSomething() {
}

I'd like to enforce T to be of type X or a sub-type of it.

Thanks.

like image 500
cdroid Avatar asked Oct 23 '14 09:10

cdroid


People also ask

What are generics Kotlin?

Generics are the powerful features that allow us to define classes, methods and properties which are accessible using different data types while keeping a check of the compile-time type safety. Creating parameterized classes – A generic type is a class or method that is parameterized over types.

How do I create a generic method on Kotlin?

Kotlin generic extension function example As extension function allows to add methods to class without inherit a class or any design pattern. In this example, we add a method printValue()to ArrayList class of generic type. This method is called form stringList. printValue() and floatList.

How do I find my generic type Kotlin?

There are no direct ways to do this in Kotlin. In order to check the generic type, we need to create an instance of the generic class<T> and then we can compare the same with our class.

What is type argument in Kotlin?

Type arguments passed for the parameters of the classifier in this type. For example, in the type Array<out Number> the only type argument is out Number . In case this type is based on an inner class, the returned list contains the type arguments provided for the innermost class first, then its outer class, and so on.

What is a generic class type in Kotlin?

Generic is a Kotlin feature that allows you to construct classes, methods, and properties that may be accessed by other types and that can be checked for all variations in classes, methods, and properties at the compilation time. In the article, we will discuss the concept of the generic class type in the Kotlin language.

What is type safety in Kotlin?

In kotlin provides higher order of variable typing and it is referred as the generics types it is the type-safety that allows to hold only single type of object it does not allow to store other object.

How to create a parameterized class in Kotlin?

Let’s say that we want to create a parameterized class. We can easily do this in Kotlin language by using generic types: We can create an instance of such a class by setting a parameterized type explicitly when using the constructor: Happily, Kotlin can infer the generic type from the parameter type so we can omit that when using the constructor:

Why doesn't Kotlin have wildcard types?

One of the trickiest aspects of Java's type system is the wildcard types (see Java Generics FAQ ). Kotlin doesn't have these. Instead, Kotlin has declaration-site variance and type projections. Let's think about why Java needs these mysterious wildcards.


3 Answers

After googling around, the correct answer would be:

fun <T : X> doSomething() {
}
like image 163
cdroid Avatar answered Oct 05 '22 19:10

cdroid


Actually out SomeType means more than "type Parameter can be SomeType or any of its subtypes" as your questions suggests.

The keyword out is Kotlin's way to say that, in this example, the MutableSet is a Producer of SomeType, i.e. it is covariant in its type parameter. As a consequence you will not be able to call methods like add(t:T), but only those which return Ts, like get():T.

Back to your question: if your method is supposed to accept types of X or its subtypes, you should use "bounds":

fun <T : X> doSomething() {
}

This is just what you need, but to make it clear again, it cannot be said to be the equivalent to your other example.

like image 40
s1m0nw1 Avatar answered Oct 05 '22 17:10

s1m0nw1


as @s1m0nw1 quoted, you can use fun <T : X> doSomething() { }

to limit T subtype of X,

additional, you can use fun <T> doSomething where T : Comparable, T : Cloneable { }

to limit T should implement both Comparable and Cloneable

as referenced here https://kotlinlang.org/docs/reference/generics.html

like image 22
Minami Avatar answered Oct 05 '22 18:10

Minami