Custom Either doesn't work

I am trying to use a code like this:

sealed class Either<Animal, Plants>
abstract class Farm<Animal>(val value: Animal) : Either<Animal, Nothing>()
abstract class Garden<Plants>(val value: Plants) : Either<Nothing, Plants>()

class MyFarm : Farm<Chicken>(rosita)

fun myFunction(value: Either<Chicken, Rose>) {

I cannot call myfunction with an instance of MySubclass, the IDE says: “Type mismatch”
Is it correct, or it’s a language bug?

Sorry, your post is unreadable and does not show the error you are getting. Could you fix it?

Sorry, edited. It is clear now?

Will this suffice?

sealed class Either<A, B>
abstract class Left<A>(val value: A) : Either<A, Nothing>()
abstract class Right<B>(val value: B) : Either<Nothing, B>()

class MySubclass<A>(valueA: A) : Left<A>(valueA)

fun <A,B> myfunction(value: Either<A, B>) {

It’s correct, not a language bug. You said “MySubclass”, and I assume you mean “MyFarm”. If you add out to the type parameter in Either, to mark the type parameter as covariant, then your code will compile.

sealed class Either<Animal, out Plants>
val f = MyFarm()

A Nothing is a subtype of Rose, but unless the type parameter is covariant, a Thing<Nothing> is not a subtype of Thing<Rose>. Probably you want to mark both type parameters.


Here is the documentation for variance in generics:

it’s correct