H folks. In “package Aquarium” I am getting the following error.
Error:(22, 5) Kotlin: Overload resolution ambiguity:
public fun prObj(msg: String, myObj: Any): Unit defined in Aquarium in file AquariumFish.kt
public fun prObj(msg: String, myObj: Any): Unit defined in Aquarium in file Main.kt
How can i disambiguate this situation at fun call without renaming the function?
<your.package>.prObj(msg,obj) should do the trick if your methods are in different packages. If they are top level functions in the same package - it is not allowed. They have the same fully qualified name in kotlin (but not in JVM).
If you are bent on placing top level functions with the same name in the same package, you can introduce namespace object like:
object AquariumFish{
fun prObj(msg: String, myObj: Any)
}
But the general idea is not to abuse top level functions.
I recommend not to read about statics if you are not coming from java background. Kotlin does not have statics in the language. What you have are naming scopes. Package is a scope since full name of class, property or function includes package name. Class or object is also a scope.
The full name of entity is <package.name>... To entities with the same name and signature (for function signature consists of parameter types and in some cases return type) obviously can’t exist simultaneously since compiler can’t distinguish between them. So in order to have two functions with the same name and parameters, you need to either place them in different packages or in different classes.
Top level functions are called that way because they are not placed inside class. They are not big deal if you are coming from procedural language like Fortran or Python. But in Java they are not allowed, so they are usually confusing for Javists.
Thanks again, SO MUCH, darksnake.
Re: “not coming from java background … coming from … Python”.
You have understood my challenge. My Java experience has been augmented by lots of Python, JavaScript and Node.js.
Thanks for your patience and insight.
Love and peace,
Joe