Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to define an extension function for a companion object of a typealias?

Tags:

kotlin

I thought typealiases were the same as the original type, just a different name.

I figure typealiases have the same references as the original type.

typealias Celsius = Double

fun Double.Companion.foo() {} // Works
fun Celsius.Companion.foo() {} // Does not work

Here, Companion is accessible from Double but Celsius gives an unresolved reference error.

like image 924
Justin Ngo Avatar asked May 24 '18 14:05

Justin Ngo


People also ask

How does Kotlin define extension function?

The extension function is declared with a prefix receiver type with method name. In the above declaration, <class_name> is a receiver type and the <method_name>() is an extension function.

How do you use the companion object in Kotlin?

To create a companion object, you need to add the companion keyword in front of the object declaration. The output of the above code is “ You are calling me :) ” This is all about the companion object in Kotlin. Hope you liked the blog and will use the concept of companion in your Android application.

Where do I put Kotlin extension?

They can be anywhere, but it seems to make sense to have extensions for a particular class in the same file and/or package. For example, extensions to String could be in StringExtensions. kt, and that could optionally be in an extensions package. Save this answer.

Can extension methods access private members Kotlin?

If the extension is defined at the top level of the class, it can access all the private variables and functions of that class. If the extension function is defined outside the class, it can not access the private variables or functions of that class.


2 Answers

No, you can't access to the companion objects via typealias. One possible workaround to create one more typealias for concrete companion:

typealias CelsiusCompanion = Double.Companion

After that you can use it as following:

fun CelsiusCompanion.foo() {}
like image 200
hluhovskyi Avatar answered Oct 18 '22 18:10

hluhovskyi


If you want to define an extension function, it is not possible as hluhovskyi already stated, but things are differently if you just want to invoke functions of a companion object.

There are two ways of accessing functions and properties within a companion object. You can either specify the access explicitely or implicitely. The implicit way works with a typealias the explicit one does not.

Consider this minimal example:

class ClassWithCompanion{
    companion object {
        fun sayHello() {
            println("Hello")
        }
    } 
}

typealias Alias = ClassWithCompanion

fun main(args: Array<String>) {
    ClassWithCompanion.sayHello() // implicit
    ClassWithCompanion.Companion.sayHello() // explicit 

    Alias.sayHello() // implicit (works)
    Alias.Companion.test() // explicit (does not work)
}
like image 23
Willi Mentzel Avatar answered Oct 18 '22 16:10

Willi Mentzel