Overload functions in Kotlin vs Java

Tbf that one’s more consistent in that the first argument is always a String. It’s not like the original code you posted (unless that’s also taken from SLF4J). I think in that case, it works in Java and probably in Kotlin by doing more specific matching first. If the first argument is a String, and the second argument is a Throwable, it picks the function with the String and Throwable argument. If the first argument is a String, and the second argument is anything that is not a Throwable, then it picks the function with String and Object argument. If your first argument is ever NOT a String, then none of the functions will match.

The problem with the code in your original post is that neither option is more specific. If the first argument is a String, and the second argument is anything that is not a Throwable, then it picks the third debug function, no problems. If the first argument IS a String, and the second argument is a Throwable, that’s when it all goes wrong. Picking the first debug function, the String argument is not an exact match, but the Throwable is. Picking the third debug function, the String argument is an exact match, but the Throwable is not. So which is the correct one?

In the SLF4J code, it’s either both arguments are an exact match, or one argument is an exact match. In your original code, it’s only ever one argument that is an exact match, so which one should be the exact match?

3 Likes