Mikael Alfredsson
12/12/2020, 10:57 AMfun <T> f(): Foo where T : Bar, T : Baz { ... }
but can I do the same with for example a map?
val map = mapOf<String, T (where T:bar, T:Baz)>
Animesh Sahu
12/12/2020, 12:29 PMinterface Interf
interface Interf2
class A : Interf, Interf2
class B : Interf, Interf2
fun main() {
val list = listOf(
A(),
B()
)
test(list)
}
fun <T> test(list: List<T>) where T : Interf, T : Interf2 {
}
It has intersectional typeAnimesh Sahu
12/12/2020, 12:32 PMMikael Alfredsson
12/12/2020, 12:36 PMinterface Interf
interface Interf2
interface Interf3
class A : Interf, Interf2
class B : Interf, Interf3
fun main() {
val list = listOf(
A(),
B() // here I want this to fail.
)
list.forEach{
val a = it as Interf
val b = it as interf2 // this will be a runtime exception
}
}
Animesh Sahu
12/12/2020, 12:38 PMAnimesh Sahu
12/12/2020, 12:39 PMMikael Alfredsson
12/12/2020, 12:40 PMAnimesh Sahu
12/12/2020, 12:40 PMMikael Alfredsson
12/12/2020, 12:42 PMMikael Alfredsson
12/12/2020, 12:44 PMlistOf<Interf2>(A(), B())
the compiler would complain in my example, but I can only do that check for one interface.Animesh Sahu
12/12/2020, 12:44 PMIn my case above, i don’t really have a function and in my real life code, the list is a global list that is being used inside another function, but its not part of the functions parameters (since its a web server route function)Best bet would be to define the list as
List<Any>
and provide the add/remove methods yourself and add constraints there.Animesh Sahu
12/12/2020, 12:45 PMMikael Alfredsson
12/12/2020, 12:45 PMlistOf<Interf & Interf2>(…)
Animesh Sahu
12/12/2020, 12:45 PMMikael Alfredsson
12/12/2020, 12:46 PM