Skip to content

Instantly share code, notes, and snippets.

@deanwampler
Created March 13, 2010 15:55
Show Gist options
  • Save deanwampler/331391 to your computer and use it in GitHub Desktop.
Save deanwampler/331391 to your computer and use it in GitHub Desktop.
// When you run "scala colors.scala", it's like you compiled the following script,
// where the contents of colors.scala is embedded inside the "new Anyref {...}".
object Script {
def main(args: Array[String]): Unit = {
new AnyRef {
// If you change "object" to "class" and remove the
// "type Color = Value" declaration, it compiles.
class Color extends Enumeration {
type Color = Value
val Empty = Value
val Blue = Value
val Yellow = Value
val Red = Value
val Green = Value
}
class Board {
val grid = Array.fill(20, 20)(Color.Empty)
}
// I added these two statements:
val b = new Board
println(b.grid)
}
}
}
object Color extends Enumeration {
val Empty = Value
val Blue = Value
val Yellow = Value
val Red = Value
val Green = Value
}
class Board {
val grid = Array.fill(20, 20)(Color.Empty)
}
/*
==== Output when I run on the cmd line is below:
% ~/Projects/scala-2.8.0.Beta1-prerelease/bin/scala colors.scala
(fragment of colors.scala):10: error: private object Color escapes its defining scope as part of type Array[Array[this.Color.Value]]
val grid = Array.fill(10, 10)(Color.Empty)
^
one error found
!!!
discarding <script preamble>
==== In REPL is fine:
% ~/Projects/scala-2.8.0.Beta1-prerelease/bin/scala
Welcome to Scala version 2.8.0.Beta1-prerelease (Java HotSpot(TM) 64-Bit Server VM, Java 1.6.0_17).
Type in expressions to have them evaluated.
Type :help for more information.
scala> object Color extends Enumeration {
| val Empty = Value
| val Blue = Value
| val Yellow = Value
| val Red = Value
| val Green = Value
| }
defined module Color
scala>
scala> class Board {
| val grid = Array.fill(10, 10)(Color.Empty)
| }
defined class Board
scala> (new Board).grid
res0: Array[Array[Color.Value]] = Array(Array(Empty, Empty, Empty, Empty, Empty, Empty, Empty, Empty, Empty, Empty), Array(Empty, Empty, Empty, Empty, Empty, Empty, Empty, Empty, Empty, Empty), Array(Empty, Empty, Empty, Empty, Empty, Empty, Empty, Empty, Empty, Empty), Array(Empty, Empty, Empty, Empty, Empty, Empty, Empty, Empty, Empty, Empty), Array(Empty, Empty, Empty, Empty, Empty, Empty, Empty, Empty, Empty, Empty), Array(Empty, Empty, Empty, Empty, Empty, Empty, Empty, Empty, Empty, Empty), Array(Empty, Empty, Empty, Empty, Empty, Empty, Empty, Empty, Empty, Empty), Array(Empty, Empty, Empty, Empty, Empty, Empty, Empty, Empty, Empty, Empty), Array(Empty, Empty, Empty, Empty, Empty, Empty, Empty, Empty, Empty, Empty), Array(Empty, Empty, Empty, Empty, Empty, Empty, Empty, Empty, Empty, Empty))
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment