Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does Kotlin allow variable declarations with the same name as a parameter inside a method?

Tags:

Why does Kotlin allow variable declarations with the same name as a parameter inside a method? Then also, is there any way to access the 'hidden' parameter?

For example:

fun main(args: Array<String>) {
    val args = Any()
}
like image 207
the_kaba Avatar asked Apr 05 '18 19:04

the_kaba


People also ask

Why does Kotlin allow variable with the same name as parameter inside the method?

It is possible because names are bound to the current scope.

How do you declare a variable to a function in Kotlin?

Kotlin uses two different keywords to declare variables: val and var . Use val for a variable whose value never changes. You can't reassign a value to a variable that was declared using val . Use var for a variable whose value can change.

What does :: mean in Kotlin?

:: is used for Reflection in kotlin. Class Reference val myClass = MyClass::class.

How do you pass parameters in Kotlin?

In Kotlin, You can pass a variable number of arguments to a function by declaring the function with a vararg parameter. a vararg parameter of type T is internally represented as an array of type T ( Array<T> ) inside the function body.


2 Answers

This is called shadowing and it is useful for decoupling your code from other parts of the system. It is possible because names are bound to the current scope.

Consider this:

You subclass a class Foo from someone else, let's say an API. In your code you introduce a variable bar. The author of Foo also updates his code and also adds a variable bar. Without the local scope, you would get a conflict.

By the way, this is also possible in other JVM bases languages including Java and commonly used within constructors or setters:

public TestClass(int value, String test) {
    this.value = value;
    this.test = test;
}

public void setFoo(String foo) {
    this.foo = foo;
}

Shadowing does not only apply to parameters, other things can be shadowed too: fields, methods and even classes.

Most IDEs will warn you about shadowing as it can be confusing.

Recommendation for our own code:

try to avoid shadowing for two reasons:

  • your code becomes hard to read as two different things have the same name, which leads to confusion.
  • once shadowed, you can no longer access the original variable within a scope.
like image 113
phisch Avatar answered Oct 01 '22 23:10

phisch


Kotlin does issue a warning about name shadowing which you can suppress with:

@Suppress("NAME_SHADOWING")
val args = Any()

Allowing for such shadowing may be handy in some cases e.g. throwing a custom exception after parameter validation:

fun sample(name: String?) {
    @Suppress("NAME_SHADOWING")
    val name = name ?: throw CustomArgumentRequiredException()
    println(name.length)
}

It is unfortunately not possible to access the shadowed variable.

It is also not possible to turn a warning into an error at the moment.

like image 35
miensol Avatar answered Oct 01 '22 23:10

miensol