Skip to content

Instantly share code, notes, and snippets.

@manjuraj
Created July 2, 2015 18:07
Show Gist options
  • Save manjuraj/e617d625b4057fe5f4dc to your computer and use it in GitHub Desktop.
Save manjuraj/e617d625b4057fe5f4dc to your computer and use it in GitHub Desktop.
private classes in scala
//
// (1). Use a trait, companion object and private class that
// implements trait within the companion object
//
sealed trait Point {
def x: Int
def y: Int
}
object Point {
private case class _Point(x: Int, y: Int) extends Point
// Without an explicit return type, you get the following error:
//
// <console>:15: error: private class _Point escapes its defining scope as part of type Point._Point
// def apply(x: Int, y: Int) = {
//
def apply(x: Int, y: Int): Point = {
_Point(x, y)
}
}
scala> Point(1,2)
res0: Point = _Point(1,2)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment