Hey guys, how can I call a function extension that...
# announcements
j
Hey guys, how can I call a function extension that is declared in another class?
k
jackmiras: the best way is to just make the function top-level
j
How can I do a top level function?
k
just put it outside the class
j
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
@jackmiras post some code
j
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
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
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
@jackmiras nope, I don't know any way to do it. just make your extension function a top-level function
j
@Andreas Sinz the top-level function actually work, thanks a lot!!