I've just spent 15 minutes looking for a stupid mistake in some Scala code. The problem was that I had shadowed a variable.
In some situations in Scala, you are allowed to shadow variables. In other words, it is sometimes legal to give a new variable the same name as an existing one. This can lead to mistakes. The following legal code illustrates how you can shadow a method input variable:
def theShadow(list :Array[String]) : Seq[String] = {
// Mistake! Inadvertently
// shadowing the input parameter:
val list = List("Asa", "nisi", "masa")
list
}
(The above is a very obvious example. When you make this mistake in real code, it will probably be in a less obvious context.)
No comments:
Post a Comment