0

I'm new to android development and still learning Kotlin so my question may be simple.
So, what i want to achieve is to have getRequest() function overloaded for multiple Query parameters.
For example this are two of my functions:

    @GET("{path}")
    fun getRequest(
        @Path("path") path: String?, 
        @Query("country") country : String, 
        @Query("apiKey") apiKey: String): Call<String>

    @GET("{path}")
    fun getRequest(
        @Path("path") path: String?,
        @Query("q") q: String,
        @Query("from") from:String,
        @Query("to") to: String,
        @Query("sortBy") sortBy: String,
        @Query("apiKey") apiKey: String): Call<String>

The problem I have is I want to have another getRequest() function with same amount of parameters as the first one
So how can I achieve this if the Query parameter has to be different

@GET("{path}")
    fun getRequest(
        @Path("path") path: String?, 
        @Query("q") q: String, 
        @Query("apiKey") apiKey: String): Call<String>
Working Pickle
  • 122
  • 3
  • 10

1 Answers1

0

Since the signatures of the methods are the same (same name, and same types of parameters in the same order), the JVM won't let you do this - it can't pick the method to call based on named arguments, those only really exist at the Kotlin language level.

You can either change some of the types (which doesn't seem viable), or use different method names for the different use cases, i.e. getRequestByCountry(), getRequestForQuery(), something like that.

zsmb13
  • 85,752
  • 11
  • 221
  • 226
  • That's what I thought, thanks for answer it helped. I'm just curious can I somehow have variable in @Query("") parameter? so that I'd be able to use same function – Working Pickle Jun 30 '20 at 08:28
  • Unfortunately not, annotation parameters have to be constant at compile time. But you could do something like providing a `QueryMap`, see [here](https://stackoverflow.com/a/26110676/4465208). – zsmb13 Jun 30 '20 at 08:30
  • Okay, I guess I'll go with the easy way and create multiple query functions. Although QueryMap will be something that I'll look into in a while. Thanks for assistance once again. :) – Working Pickle Jun 30 '20 at 08:35