# Multiple type class

**URL:** <https://discuss.kotlinlang.org/t/multiple-type-class/25884>\
**Category:** Support\
**Created:** [October 28, 2022, 10:25am UTC](https://discuss.kotlinlang.org/t/multiple-type-class/25884 "2022-10-28T10:25:03Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Psijic](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/psijic/32/2227_2.png) [@Psijic](https://discuss.kotlinlang.org/u/Psijic)\
**Post date:** [October 28, 2022, 10:25am UTC](https://discuss.kotlinlang.org/t/multiple-type-class/25884/1 "2022-10-28T10:25:03Z")

</div>

Trying to pass a class as 2 different types merged in a top-level parent. How to specify types correctly (not like `a as B`, needs a sure shot)?

```auto
class Foo<T>(foo: T) : Buz(foo = foo) {
    fun <T> bar(kyle: Kyle)
            where T : Chad,
                  T : Bar {
        kyle.foo = foo
    }
}

class Buz(val foo: Chad)

class Kyle(var foo: Bar)

```

---

<div class="post-metadata">

**Author:** ![Psijic](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/psijic/32/2227_2.png) [@Psijic](https://discuss.kotlinlang.org/u/Psijic)\
**Post date:** [October 28, 2022, 2:12pm UTC](https://discuss.kotlinlang.org/t/multiple-type-class/25884/2 "2022-10-28T14:12:24Z")

</div>

The variant is to create a common interface but it can be enormous for multiple combinations. What about `where`?

---

<div class="post-metadata">

**Author:** ![KamilDoCholery](https://avatars.discourse-cdn.com/v4/letter/k/90db22/32.png) [@KamilDoCholery](https://discuss.kotlinlang.org/u/KamilDoCholery)\
**Post date:** [October 28, 2022, 7:06pm UTC](https://discuss.kotlinlang.org/t/multiple-type-class/25884/3 "2022-10-28T19:06:30Z")

</div>

I’m not sure if I understood you correctly, but I think you are describing intersection types. They are not available in kotlin yet, however there is syntax for it, you can use `&` to create intersection type with `Any` to force non-nullable version of a type, eg

```auto
fun <T> T.nonNull(): T & Any = this ?: throw NullPointerException()

```

And interesection types can be infered, you can see this syntax in IDEs eg. in Intellij IDEA when you enable inline hints and do something like this `val foo = if(true) 1 else 0f` you can see that infered type is `Comparable<*> & Number`, so I guess its the matter of time before intersection types will be implemented properly

---

<div class="post-metadata">

**Author:** ![Psijic](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/psijic/32/2227_2.png) [@Psijic](https://discuss.kotlinlang.org/u/Psijic)\
**Post date:** [November 4, 2022, 10:46am UTC](https://discuss.kotlinlang.org/t/multiple-type-class/25884/4 "2022-11-04T10:46:53Z")

</div>

Suppose, something like this could solve the problem

> val foo: Bar && Buz

---

<div class="post-metadata">

**Author:** ![kyay10](https://avatars.discourse-cdn.com/v4/letter/k/c57346/32.png) [@kyay10](https://discuss.kotlinlang.org/u/kyay10)\
**Post date:** [November 7, 2022, 5:48am UTC](https://discuss.kotlinlang.org/t/multiple-type-class/25884/5 "2022-11-07T05:48:31Z")

</div>

Note that T cannot extend 2 classes at the same time, unless the classes extend each other, hence those classes should actually be interfaces.

```run-kotlin
interface Chad
interface Bar
class Foo<T>(val fooParam: T) : Buz(foo = fooParam) 
    where T : Chad, T: Bar
{
    fun bar(kyle: Kyle) {
        kyle.foo = fooParam
    }
}

open class Buz(val foo: Chad)

class Kyle(var foo: Bar)

```

---

<div class="post-metadata">

**Author:** ![Psijic](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/psijic/32/2227_2.png) [@Psijic](https://discuss.kotlinlang.org/u/Psijic)\
**Post date:** [November 8, 2022, 4:41pm UTC](https://discuss.kotlinlang.org/t/multiple-type-class/25884/6 "2022-11-08T16:41:22Z")

</div>

Yeah, I found the problem with the context scope and you need to override  
`class Foo<T>(override val foo: T) : Buz(foo = foo)`
