Hi, can we respond to a unknown method call in Kot...
# announcements
g
Hi, can we respond to a unknown method call in Kotlin? For example, on the Rosetta Code we have a lot of options for different languages but the Kotlin version is using a javascript method only. https://rosettacode.org/wiki/Respond_to_an_unknown_method_call#Kotlin What I’m looking for is the
method_missing
of ruby, the
__call__
in PHP, and so on. I’ve heard about delegation in Kotlin, but I’m not so sure we can achieve the same result, for example, something like:
MyClass().unknownMethod()
// returns, for example, “You’ve called the `unknownMethod`“. Can someone give me an small example how can we achieve that? Thanks in advance
c
That is mostly a feature of dynamic languages. In Kotlin attempt to access non-existing method is a compile time error.
m
There is, however some support for dynamic method resolution in the JVM: https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/lang/invoke/package-summary.html
g
@reik.schatz said to me that there is a Dynamic type in Scala that can do that, so this should be possible in Kotlin too, since they are two jvm languages.
Oh, interesting, maybe this Proxy approach can work like the Javascript approach that we can see on the rosetta code. Also, interesting this thing about a dynamic type in java 11.
c
Scala is a very different language, it has a lot of stuff that Kotlin does not.
g
What I would like to do is to create an api controller that could call any of the implemented methods of a business object of mine, since I have a lot of possible methods an dynamic way will suit very well.
c
That's perfectly possible, you'll have to use reflection though.
e.g. (pseudo-code)
Copy code
@Path("/api/.../{method}")
fun callAnyMethod(@PathParam("method") methodName: String) {
    beanWithALotOfMethods
        .methods
        .findByName(methodName)
        .invoke()
}
g
Hum, I think this indeed can work for me and solve my problem. Thanks 😃 Either way, seeing this conversation, maybe it is possible to us create an extension in the Any class in Kotlin with some special method, for example
__noSuchMethod__
to handle this kind of things. The only problem is how can we tell the Kotlin compiler to use this method when the method is not a known one. What do you guys think?
r
I don’t know, it defeats the purpose of a static typed language no? suddenly I can call everything on anything
c
That's a language feature, you could write a proposal for it, but I don't think it would gain any traction. I'd say not having this feature is itself a feature as far as Kotlin is concerned.
g
Hum, I think I’ll file an issue about this later. I don’t think this should be the way to go always, it need to be avoided, but sometimes it is necessary. For example, the kotlin team realized that this was necessary for the js world, and I think it can have good use cases in the jvm world too.
c
With JS it's a bit of a different situation, as JS itself is dynamic, therefore having something dynamic for it to interoperate with was necessary. But if you feel strongly about it, sure, create an issue, it could at least spawn an interesting discussion.