# Accessing companion members from instance?

**URL:** <https://discuss.kotlinlang.org/t/accessing-companion-members-from-instance/10926>\
**Category:** Uncategorized\
**Created:** [December 27, 2018, 11:52am UTC](https://discuss.kotlinlang.org/t/accessing-companion-members-from-instance/10926 "2018-12-27T11:52:09Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![pglez](https://avatars.discourse-cdn.com/v4/letter/p/a87d85/32.png) [@pglez](https://discuss.kotlinlang.org/u/pglez)\
**Post date:** [December 27, 2018, 11:52am UTC](https://discuss.kotlinlang.org/t/accessing-companion-members-from-instance/10926/1 "2018-12-27T11:52:10Z")

</div>

Say I have a Foo class with bar() method, and several classes, A, B, C, whose companion inherits from Foo. Now, I have a variable, x, and I know it is of type A, B or C (but not exactly which one). I would like to call bar() from x’s companion. How would I do this?

I tried x.bar() (which would be ideal for me) and x.Companion.bar() and neither compile.What’s the idiomatic way to do this?

Note: I’m aware I could do a switch so if x is A then A.bar(), etc, but that’s highly unpractical.

---

<div class="post-metadata">

**Author:** ![fatjoe79](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/fatjoe79/32/4221_2.png) [@fatjoe79](https://discuss.kotlinlang.org/u/fatjoe79)\
**Post date:** [December 27, 2018, 6:53pm UTC](https://discuss.kotlinlang.org/t/accessing-companion-members-from-instance/10926/2 "2018-12-27T18:53:00Z")

</div>

Give A,B,C a common super interface Fooable that has a property val foo of type Foo. Let them return their own companions in this property. Then use `fooable.foo.bar()`. More specifically, your variable x needs to be of type Fooable. That way you can retrieve it’s foo property.

---

<div class="post-metadata">

**Author:** ![pglez](https://avatars.discourse-cdn.com/v4/letter/p/a87d85/32.png) [@pglez](https://discuss.kotlinlang.org/u/pglez)\
**Post date:** [December 27, 2018, 7:41pm UTC](https://discuss.kotlinlang.org/t/accessing-companion-members-from-instance/10926/3 "2018-12-27T19:41:15Z")

</div>

So, there’s no general way to go from an object to it’s class companion out of the box? An implementation has to bre provided in each class to do that?

Also, any idea why is this a problem?

```auto
 class Myclass {
   companion object {}
   val comp = Companion //error, says I need getter. Why?
   val comp2 
     get() = Companion //this is fine though
 }
```

---

<div class="post-metadata">

**Author:** ![fatjoe79](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/fatjoe79/32/4221_2.png) [@fatjoe79](https://discuss.kotlinlang.org/u/fatjoe79)\
**Post date:** [December 27, 2018, 7:53pm UTC](https://discuss.kotlinlang.org/t/accessing-companion-members-from-instance/10926/4 "2018-12-27T19:53:01Z")

</div>

> [@pglez](#):
>
> So, there’s no general way to go from an object to it’s class companion out of the box?

That’s not the main problem that you are facing. The main problem is that your variable x needs to be of some type that you can work with. If its type is `Any` you will not be able to achieve anything meaningful with it.

Even if there were a way to retrieve the companion of Any variables, what type would that companion reference be? It would need to be of type Any (more specifically Any?). This means you wouldn’t be able to invoke your custom member functions on that companion reference.

The solution is to have a variable x of type Fooable, which allows us to access the foo property.

You are probably coming from a dynamicly typed language, aren’t you? Javascript maybe?

> Also, any idea why is this a problem?

I can only make assumptions here. Nothing that I am confident enough to share.

---

<div class="post-metadata">

**Author:** ![pglez](https://avatars.discourse-cdn.com/v4/letter/p/a87d85/32.png) [@pglez](https://discuss.kotlinlang.org/u/pglez)\
**Post date:** [December 27, 2018, 8:35pm UTC](https://discuss.kotlinlang.org/t/accessing-companion-members-from-instance/10926/5 "2018-12-27T20:35:26Z")

</div>

The problem is not with coming from dynamic languages at all… My train of thought was more like: “I know at compile time that a given variable’s type has a companion with a given method, so the compiler should be able to ‘go’ from the variable to the method wothout me explicitly telling it that it can go there”.

Anyway, I guess that if I have a member val pointing to the companion, I can do something like `class MyClass: IAccompanied by companion`.

---

<div class="post-metadata">

**Author:** ![fvasco](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/fvasco/32/1961_2.png) [@fvasco](https://discuss.kotlinlang.org/u/fvasco)\
**Post date:** [December 28, 2018, 10:00am UTC](https://discuss.kotlinlang.org/t/accessing-companion-members-from-instance/10926/6 "2018-12-28T10:00:20Z")

</div>

I consider this as a root problem

> [@pglez](#):
>
> I know it is of type A, B or C

Kotlin not

---

<div class="post-metadata">

**Author:** ![SackCastellon](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/sackcastellon/32/4553_2.png) [@SackCastellon](https://discuss.kotlinlang.org/u/SackCastellon)\
**Post date:** [December 28, 2018, 11:56am UTC](https://discuss.kotlinlang.org/t/accessing-companion-members-from-instance/10926/7 "2018-12-28T11:56:05Z")

</div>

You can use Kotlin reflection to accomplish what you want, but you’ll need [`kotlin-reflect`](https://mvnrepository.com/artifact/org.jetbrains.kotlin/kotlin-reflect):

```kotlin
import kotlin.reflect.full.companionObjectInstance

abstract class Foo {
    abstract fun bar(): String
}

class A {
    companion object : Foo() {
        override fun bar() = "I'm the companion of A"
    }
}

class B {
    companion object : Foo() {
        override fun bar() = "I'm the companion of B"
    }
}

class C {
    companion object : Foo() {
        override fun bar() = "I'm the companion of C"
    }
}

fun main() {
    val x: Any = listOf(A(), B(), C()).random() // An instance of A, B or C
    val whoAreYou = (x::class.companionObjectInstance as Foo).bar()
    println(whoAreYou)
}

```

In this code you are sure `x` is one of A, B or C so theoretically there should be no problem when casting to `Foo`, but in case you cannot ensure that just replace the cast with a safe cast:

```auto
val whoAreYou = (x::class.companionObjectInstance as? Foo)?.bar()

```

---

<div class="post-metadata">

**Author:** ![fatjoe79](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/fatjoe79/32/4221_2.png) [@fatjoe79](https://discuss.kotlinlang.org/u/fatjoe79)\
**Post date:** [December 28, 2018, 12:57pm UTC](https://discuss.kotlinlang.org/t/accessing-companion-members-from-instance/10926/8 "2018-12-28T12:57:56Z")

</div>

He asked for an idiomatic way. Using reflection allows for code that is similar to how it would work in a dynamicly typed language. It is for sure not an idiomatic Kotlin solution.

---

<div class="post-metadata">

**Author:** ![SackCastellon](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/sackcastellon/32/4553_2.png) [@SackCastellon](https://discuss.kotlinlang.org/u/SackCastellon)\
**Post date:** [December 28, 2018, 1:37pm UTC](https://discuss.kotlinlang.org/t/accessing-companion-members-from-instance/10926/9 "2018-12-28T13:37:42Z")

</div>

Oh, I didn’t see that 😅 but you are right.  
However, I just wanted to show that what he asks is possible, even though it uses reflection.

---

<div class="post-metadata">

**Author:** ![fatjoe79](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/fatjoe79/32/4221_2.png) [@fatjoe79](https://discuss.kotlinlang.org/u/fatjoe79)\
**Post date:** [December 28, 2018, 1:44pm UTC](https://discuss.kotlinlang.org/t/accessing-companion-members-from-instance/10926/10 "2018-12-28T13:44:10Z")

</div>

Fair enough 😉

If the target platform is Javascript it would also be possible without reflection, by using Kotlin’s dynamic type. However, this also is not a nice idiomatic solution.
