Let's say, there 2 functions with same signature b...
# announcements
k
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
why would u have two functions with the same signature but one without a default parameter?
k
Please check the link
h
what is a real life purpose to do that?
k
Nothing, but the question is that why did not the compiler at least warn?
h
if you provide no args, it will pick the one with no arguments, otherwise the one with arguments
k
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
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