Looking through the Scala 2.8 Predef class, I find that there is a method "locally". As near as I can tell, it's the same as Predef.identity, except for having the "@inline" annotation. What's it for, and why is it important enough to be in Predef (and thus usable anywhere in Scala)?
Asked
Active
Viewed 5,412 times
43
-
4See: http://www.scala-lang.org/node/3594 – retronym Jul 13 '10 at 14:20
-
Wow, I was actually a part of that thread and forgot all about it. If you make this an answer rather than a comment, I'll accept it and close the question – Dave Griffith Jul 13 '10 at 14:25
-
2Hmm. I'd still be interested what's the difference between `identity` and `locally`. – mkneissl Jul 16 '10 at 22:04
1 Answers
49
It is discussed here: http://www.scala-lang.org/node/3594
The idea was to avoid the programmer error of confusing a 'dangling' local block with the template of an object/class/trait.
object test {
object a
{
val x = 1
}
object b
{ // oops, extra newline disassociates this block with the object b
val x = 1
}
}
test.a.x
//test.b.x // doesn't compile
If the programmer really wants that block to stand alone, locally
could be used:
object test {
object a
{
val x = 1
}
object b
locally {
val x = 1
}
}
This thread also suggested that the first code would produce a deprecation warning. This has not yet been added.

retronym
- 54,768
- 12
- 155
- 168