Created
July 2, 2015 18:07
-
-
Save manjuraj/e617d625b4057fe5f4dc to your computer and use it in GitHub Desktop.
private classes in scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// (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