In Scala, what explains the possibility of parameter shading?

This turns out to be completely acceptable for the compiler (at least in 2.10.3 and 2.11-M7):

def foo(n: Int) = {
  val n = 3
  n * 3
}

... this is probably because the parameter exists in the outer region of the body of the method / function, which is a technical argument, but it can lead to problems efficiently (as I just found out in the real life code), so I wonder if is it just an inevitable consequence of language design or if it really serves a real (higher?) purpose.

PS it's even OK to use a different type for the shadow name:

def foo(n: Int) = {
  val n = "hello"
  n * 3
}

Note: the existing question asks a similar, but still conceptually completely different question: Why does Scala support shadow variables? -that one of them asks about the shadow name as a whole, while I am concerned about the fact that shading (unexpectedly) occurs with parameters, and also when there is no obvious table of contents, - yes, there are curly brackets, but one The former (possibly) assumes the parameters are in the same area.

EDIT: Haskell, an example language, or FP also allows: foo x = let x = 4 in xis completely legal.

+3
source share
1 answer

Sometimes languages ​​contain functions not because they are considered good and useful, but because no one takes them into account at all.

+1
source

All Articles