Coming from Scala, the lack of shadowing in CoffeeScript seems very odd. I wrote a demo.
object ZekeDemo extends App {
val filterAll = (arr: Seq[String]) => {
val saved = ArrayBuffer[String]()
val removed = ArrayBuffer[String]()
val filterDo = (arr: Seq[String]) => {
val saved = for {item <- arr if item != "do"} yield item
val removed = for {item <- arr if item == "do"} yield item
(saved, removed)
}
val filterSo = (arr: Seq[String]) => {
val saved = for {item <- arr if item != "so"} yield item
val removed = for {item <- arr if item == "so"} yield item
(saved, removed)
}
val addRemoved = (item: String) => removedBuff += item
val addSaved = (item: String) => savedBuff += item
for {item <- filterDo(arr)._1} { addSaved(item)}
for {item <- filterDo(arr)._2} { addRemoved(item)}
for {item <- filterSo(arr)._1} { addSaved(item)}
for {item <- filterSo(arr)._2} { addRemoved(item)}
(saved, removed)
}
val song = Seq("do", "re", "mi", "fa", "so")
val s = filterAll(song)._1
val r = filterAll(song)._2
println("saved: %s, removed: %s".format(s.mkString(","), r.mkString(",")))
}
Now here's the same program in CoffeeScript:
filterAll = (arr) ->
saved = []
removed = []
filterDo = (arr) ->
saved = ->
item for item in arr when item != "do"
removed = ->
item for item in arr when item == "do"
{"saved":saved(), "removed":removed()}
filterSo = (arr) ->
saved = ->
item for item in arr when item != "so"
removed = ->
item for item in arr when item == "so"
{"saved":saved(), "removed":removed()}
addRemoved = (item) ->
saved[saved.length] = item
addSaved = (item) ->
removed[removed.length] = item
addRemoved item for item in filterDo(arr)["removed"]
addSaved item for item in filterDo(arr)["saved"]
addRemoved item for item in filterSo(arr)["removed"]
addSaved item for item in filterSo(arr)["saved"]
{"saved":saved, "removed":removed}
song = ["do", "re", "mi", "fa", "so"]
s = filterAll(song)["saved"]
r = filterAll(song)["removed"]
alert("saved: " + s + ", removed: " + r)
The "saved" that is declared as an array gets overwritten by the "saved" that references the for comprehension. Changing the variable names yields the expected output.
This seems very strange to me. A big part of the beauty of functional languages is not needing to know about outer scopes. If I write my "filterDo" function in another context (class|function|file), I believe I should be able to drop it into any other context where it is valid syntax and not have to worry about whether it is stepping on values from an outer scope.
A language that requires a developer to know all variable names in scopes outside the current scope discourages its developers from encapsulation. I could have fixed this bug by moving filterDo and filterSo to the outer scope. But this pollutes that scope's namespace and unnecessarily increases the surface area of the interface.
CoffeeScript's argument for not having a special syntax for shadowing variables is that you simply shouldn't do this kind of thing. Name your variables clearly. Because even if shadowing would be allowed it would be very confusing to have two variables with two different meanings with the same name, one in an inner scope and one in an enclosing scope.
I like this idea in principle, but in practice I don't believe languages should exhibit behavior this difficult to trace and resolve based on rules this subtle. Variable naming is a reflection of coding style, and style choices should not change the behavior of a program.