Does anybody know, for what reason this one <https...
# announcements
a
Does anybody know, for what reason this one https://pl.kotl.in/Sk2RqPdEN does not throw compile-time error?
t
Seems like it executes the function with less arguments if both have defaults for all https://pl.kotl.in/HJ18AwuV4
👆 1
a
what were you expecting?
a
Error
a
I know! what error?
a
Compile time error. The same that we will see if we remove second argument in "A" function
t
I think I agree with expecting an error, don't see how this quirk of the compiler helps
2
a
The call is obviously ambiguous. Basic documentation says nothing about this behavior (at least I did not find anything)
The only thing I found is very old issue in tracker that also does not answer the question: https://youtrack.jetbrains.com/issue/KT-9371
a
It's rather magical indeed, funny though it's marked as resolved
a
It is probably somehow connected to the resolution of function with lambdas, but still, without proper reason in documentation, it just seems strange
a
I think you should open an issue on YT, it either must be fixed or documented.
a
Yep. I am searching for something similar. Maybe it is there already.
a
Would you also expect this to be ambiguous?
Copy code
fun position(arg: Int = 0){println("B")}
fun position(arg: Int = 0, arg2:Int = 1) {println("A")}

fun main(){
    position(1)
}
a
Yes, I think so.
👍 1
Yet there is a rule that method without parameters win against method with default parameter. This one is quite similar, so, there could be different opinions
This rule is also not documented
a
The issue points to a commit, https://github.com/jetbrains/kotlin/commit/d015f713cbf14fdbf76ed917e3b92a631e956f85 If I'm not reading that wrong, it is testing for ambiguity in this case:
Copy code
fun wrongTwoDefault(a: Any = 2) = 1
fun wrongTwoDefault(a: Int = 2, b: String = "") = ""
 <!OVERLOAD_RESOLUTION_AMBIGUITY!>wrongTwoDefault<!>(1)
However, there is no ambiguity error in that case. Now at least.
a
So it is some kind of regression?
OK, I did not find an issue, so I will write one.
✔️ 1
r
I actually ran into this a while ago. Turns out there is an established rule: The function with the least number of parameters (in the bytecode) wins.
a
It seems so. But I did not found it in the documentation.
Maybe I've searched in the wrong place. And there should be a warning.
r
I can't remember where I found that rule. Maybe it was just in a discussion with the benevolent JB overlords.
a
I just found it by experimenting. It is rather simple, but not obvious.
r
Indeed, but I remember at some point I found out it is a well established and intentional rule. Unfortunately I don't remember the details as to why or where.
a
Those issues seems to be awarded to @stanislav.erokhin. We can try to summon his spirit here.
s
No, it isn't a regression. In initial case one function use 2 defaults and another - 1 default. So function that used less defaults - wins. In case https://kotlinlang.slack.com/archives/C0922A726/p1549463900076800?thread_ts=1549462913.072800&amp;cid=C0922A726 by previous rule first function should win, but for used parameters we compare type specifisity and
Int
should win over
Any
. So none of function usn't specific that another.
I'm not sure that we should cover this in documentation, the more suitable place for this - language specificatuon. Draft of this part you can find here: https://github.com/JetBrains/kotlin-spec/blob/spec-rework/src/kotlin.core/overload-resolution.md#choosing-the-most-specific-function-from-the-overload-candidate-set
a
I think there should be at least a link in the documentation on defaults. The point is not obvious. Especially if some pure soul working without IDE
a
image.png
Please explain, if I didn't read that wrong, how come I don't see the issue the source seems to suggest I should ^
Does this line
<!OVERLOAD_RESOLUTION_AMBIGUITY!>wrongTwoDefault<!>(1)
Not say that there should be an ambiguity error at this point?
wrongWithDefaultGeneric
from that test also does not fail @stanislav.erokhin
s
O. I’m sorry, I was wrong. We compare
Int
and
Any
at first and because
Int
is more specific that
Any
we chose second candidate. but if types are equal, then we compare by defaults count.
a
OK, but doesn't your test look like it expects those to fail? @stanislav.erokhin but they don't