https://kotlinlang.org logo
#getting-started
Title
# getting-started
d

dvdandroid

10/27/2021, 1:20 PM
given this:
public fun <T> myFun(vararg arg: T) where T : MyClass, T : MyInterface
how can i create collection<T> (outside this function)? what i should achieve is:
val x: Set<T> = setOf(a, b)
or like
val x: Set<MyClass & MyInterface> = setOf(a, b)
if i remove the explicit type it works, but i need it for a function return type (type inference errors)
r

Ruckus

10/27/2021, 2:44 PM
Intersection types are not denotable in Kotlin, even though they exist in the type system. Hopefully that will change someday.
s

Stephan Schroeder

10/27/2021, 2:57 PM
like Ruckus said, your code basically works: https://pl.kotl.in/sce5crCw- But and-ing types with
&
, like in`Set<MyClass & MyInterface>`, doesn't exists. I was already surprised that I could double specify it for the generic function argument 😅
s

smit01

10/27/2021, 9:34 PM
• open class A • interface C • val <T> T.value:Set<T> where TA,TC get()=setOf(this)
3 Views