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

jackmiras

02/20/2017, 4:10 PM
Hey guys, how can I call a function extension that is declared in another class?
k

kirillrakhman

02/20/2017, 4:10 PM
jackmiras: the best way is to just make the function top-level
j

jackmiras

02/20/2017, 4:11 PM
How can I do a top level function?
k

kirillrakhman

02/20/2017, 4:12 PM
just put it outside the class
j

jackmiras

02/20/2017, 5:24 PM
I've done this but I'm having some problems in call the function.
Even if I specify the package of the class that holds the extended function the IDE doesn't find the function.
a

Andreas Sinz

02/20/2017, 5:37 PM
@jackmiras post some code
j

jackmiras

02/20/2017, 5:38 PM
I have this class that has a extension function:
Copy code
class RequestExtentions {

    fun Request.bodyObject(): Any {
        try {
            return Parser().toKotlin(body())
        } catch (e: Exception) {
            return e
        }
    }

}
I would like to call the function above in another class just like the code down below:
Copy code
override fun post(req: Request, res: Response): Any {
        val platform = req.bodyObject()
    }
The problem is that when I try to do this val platform = req.bodyObject() I can't import the bodyObject function
a

Andreas Sinz

02/20/2017, 5:43 PM
i'm not sure this is possible. why not put the extension method outside of the class?
or put the extension method next to your
fun post(..)
j

jackmiras

02/20/2017, 5:48 PM
The extension function was next to the
fun post(...)
but this function will repeat along other classes.
That's why I would like to move the function to the
class RequestExtentions {...}
@Andreas Sinz do you know if there is a way to make this work?
a

Andreas Sinz

02/20/2017, 5:59 PM
@jackmiras nope, I don't know any way to do it. just make your extension function a top-level function
j

jackmiras

02/20/2017, 6:31 PM
@Andreas Sinz the top-level function actually work, thanks a lot!!
2 Views