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

KindRoacher

07/26/2019, 8:37 AM
Let's say, there 2 functions with same signature but one is with a default parameter and the other is not. When invoking one of the function, how does the compiler figure out which one to pick? because when invoking the function, we have then 2 choice, either call one without the default parameter or one with the default parameter. But even if we invoke the function with the default parameter, somehow the compiler still invokes the function without the default parameter. I checked the bytecode but I could get much answer from it. https://pastebin.com/nqfsFLHH
h

Hamza

07/26/2019, 8:38 AM
why would u have two functions with the same signature but one without a default parameter?
k

KindRoacher

07/26/2019, 8:39 AM
Please check the link
h

Hamza

07/26/2019, 8:39 AM
what is a real life purpose to do that?
k

KindRoacher

07/26/2019, 8:40 AM
Nothing, but the question is that why did not the compiler at least warn?
h

Hamza

07/26/2019, 8:41 AM
if you provide no args, it will pick the one with no arguments, otherwise the one with arguments
k

KindRoacher

07/26/2019, 8:43 AM
I tried to search for this behavior in the documentation and I could not find it. I checked the bytecode and apparently, the compiler generates a static method like this
Copy code
public static void doSomething$default(TestDuplicates var0, String var1, int var2, Object var3) {
      if ((var2 & 1) != 0) {
         var1 = "";
      }

      var0.doSomething(var1);
   }
m

Marat Akhin

07/26/2019, 9:27 AM
At the moment, the appropriate overload resolution rule for your situation is: if everything else is the same, pick the function candidate with the least number of used default argument values. If you're brave enough, you could read these docs for further details on overload resolution: https://kotlin.github.io/kotlin-spec/#overload-resolution NB: work in progress, may be incorrect here or there
13 Views