Strings in scala can be used as functions from index to char at a given index:
val s: Int => Char = "abcd" val c: Char = s(1)
This is a common mechanism in scala where an object with an apply method can be thought of as a function. The apply method for strings is defined in StringOps .
The string "asdfsdff" is passed to foreach, and each subsequent value in the range is passed to the function. This throws an exception when the index reaches 8 , since it is out of range.
source share