Using framework with intersection type in method

Hi there,
I can’t find a way to use a method from the CUBA platform that has the following method declaration: (Screen is a class and LookupScreen is an interface)

public <S extends Screen & LookupScreen<E>> LookupClassBuilder<E, S> withScreenClass(Class<S> screenClass)

Now I have a class that looks like this:

class Test(val browseClass: Class<Any>?)

and a method where I want to call the method from above:

            screenBuilders.lookup(field)
                    .withScreenClass(browseClass)

… but that doesn’t compile, because of a type mismatch and I can’t find a way to correctly declare the browseClass

Any suggestions?

You need a where clause if you have to specify multiple constraints for the same type parameter: https://kotlinlang.org/docs/tutorials/kotlin-for-py/generics.html#constraints

1 Like

I couldn’t find a correct way to declare the type parameter with a where clause… It seems that only class type parameters (not constructor types) can be declared via a where

This works for me:

private class X <T> constructor(private val t: T) where T: CharSequence, T: String

fun y() {
    X("A string works")
   // But other implementations of "CharSequence" do not
    X(object: CharSequence {
        override val length: Int
            get() = TODO("Not yet implemented")

        override fun get(index: Int): Char {
            TODO()
        }

        override fun subSequence(startIndex: Int, endIndex: Int): CharSequence {
            TODO()
        }
    })
}
1 Like

Thanks, I forgot to add the generic <T> in the class declaration. :see_no_evil:

1 Like