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.
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.
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.
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.
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.
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.
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.
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:
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.
After googling around, the correct answer would be:
fun <T : X> doSomething() {
}
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 T
s, 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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With