3

This turns out to be completely acceptable to 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 scope of the method/function body, which is the technical reasoning, but effectively, this can lead to problems (as I've just found out in real life code), so I'm wondering if this is just an unavoidable consequence of the language design, or if it actually serves a real (higher?) purpose.

P.S. it's even OK to use a different type for the shadowing name:

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

Note: an existing question asks a similar but still conceptually very different question: Why does Scala support shadow variables? —that one asks about name shadowing in general, whereas I'm concerned with the fact that shadowing (unexpectedly) happens with parameters as well, where no obvious sub-scoping occurs—yes, there are the curly brackets, but one still (arguably) assumes the parameters are in the same scope.

EDIT: Haskell, the exemplar or FP languages, also allows this: foo x = let x = 4 in x is perfectly legal.

Community
  • 1
  • 1
Erik Kaplun
  • 37,128
  • 15
  • 99
  • 111
  • 1
    I do not see why it cannot be possible. At best, the compiler might issue a warning about the unused parameter. Also, because you do not explicitly set the return type, compiler just guesses it from the inner expression. – Ashalynd Feb 05 '14 at 14:46
  • 1
    @Ashalynd: nowhere do I state that it's impossible—quite the contrary: I'm asking **why** the compiler silently allows such construction. Also, the inference of the return type is completely unrelated to the question. – Erik Kaplun Feb 05 '14 at 14:48
  • 3
    I don't know why... However if this behavior bit you, there's a `-Ywarn-shadowing` advanced option of scalac that should at least warn you – Paolo Falabella Feb 05 '14 at 14:50
  • http://stackoverflow.com/questions/7031106/why-does-scala-support-shadow-variables – Dave L. Feb 05 '14 at 14:51
  • @david: thanks; perhaps I was too hasty but I couldn't find that question before posting this one! **P.S.** actually, on second thought: the other question asks about variable shadowing in general, which I have no objections to whatsoever—my question is about the fact that it works also with parameters (i.e. where no obvious sub-scope is visible). – Erik Kaplun Feb 05 '14 at 14:52
  • @PaoloFalabella: btw, unable to find a `-Ywarn-shadowing` option to `scalac`. – Erik Kaplun Feb 05 '14 at 15:22
  • 1
    @ErikAllik looks like you're right, it must have been removed. I only found https://issues.scala-lang.org/browse/SI-4762 that did not seem to get any attention after 2012, so it would look like its a known issue but not considered terribly high-priority... – Paolo Falabella Feb 05 '14 at 15:49
  • @PaoloFalabella: thanks—good to know that it's actually considered a problem not a neutral/desireable trait. – Erik Kaplun Feb 05 '14 at 20:58
  • That ticket is a different issue but I suggested -Ywarn-unused could profitably warn in your case. – som-snytt Mar 13 '14 at 17:34
  • RTFW - Read The F*** Warnings :) – dk14 Apr 09 '15 at 08:24

1 Answers1

1

Sometimes languages contain features not because they are considered to be good and useful, but because no one considered at all.

TwoThe
  • 13,879
  • 6
  • 30
  • 54