https://kotlinlang.org logo
#announcements
Title
# announcements
z

zceejkr

11/10/2019, 4:34 PM
Hi, one question if I may: I have a sealed class Foo that is extended by A, B and C and a generic interface Bar<T>. Bar only requires one method that returns T. I want to make an object of type Bar<Foo>, that will return one of the Foo members. But if I put the type Bar<Foo> and try to return A in the required method, I get a type error (it wants Foo, not A). If I put the type Bar<out Foo>, i get this error:
Projections are not allowed for immediate arguments of a supertype
. I am new to generics so if anyone can point me in the right direction I would be thankful.
s

streetsofboston

11/10/2019, 4:38 PM
Did you try to declare Bar as follows, with a covariant generic param
T
(out on the declaration site):
interface Bar<out T> { fun oneMethodThatReturnsT(): T }
?
Also, if you could provide a snippet of the code that doesn’t compile would really help 🙂
z

zceejkr

11/10/2019, 6:17 PM
Ok so I have done so and it works now (on JVM). But the same code does not work when compiled to JavaScript. Not a compile time error, it does not work at runtime. The example is somewhat convoluted and I think it might be a bug with kotlinc-js. Do you know where I could report such a bug?
k