Scala Question Regarding readLine

Patrick Jayet
XRB’s Blog
Published in
1 min readJul 10, 2010

I have stumbled upon a weirdness regarding the behavior of readLine in Scala (at least with my current basic understanding of the language).

The following snippet is supposed to read three times a line from standard in. But it reads 3 times the same line (the first).

[code]
(1 to 3) foreach {
val line = Console.readLine
i => println(line)
}
[/code]

while the following snippet (very similar) behaves as expected (reading the three first line from stdin):

[code]
(1 to 3) foreach {
i => println(Console.readLine)
}
[/code]

Any idea why?

--

--

Patrick Jayet
XRB’s Blog

Polyglot (FR, DE, EN, ES), polyglot programmer (Java, Groovy, Ruby, Swift, Objective-C, Scala, Python, O’Caml) polyglot methodologist (Scrum, Kanban, Lean).