Issue
i have query that is it fine to use to use named parameter which having name parameter and the variable name same in a function call
for example
fun fun1( to:string ){
//body}
fun fun2( ){
to = "xyz"
fun1( to = to)
}
calling fun1 which have a parameter name "to" and passing variable "to" is it okay to do so . In either of case can someone also explain how the compiler will resolve this
Solution
In Kotlin, property or variable assignment can only happen if the variable/property name and =
are at the beginning of a line of code (or the first code in the scope { }
of a lambda or defined function), not in the middle of an expression or list of function arguments.
Since the compiler sees to =
inside the argument list of a function call, it resolves it as a named argument rather than a variable assignment, so the to
of to =
is interpreted as a function parameter name.
Answered By - Tenfour04
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.