Skip to content

Instantly share code, notes, and snippets.

@v6ak
Created September 5, 2011 14:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save v6ak/1195101 to your computer and use it in GitHub Desktop.
Save v6ak/1195101 to your computer and use it in GitHub Desktop.
package com.v6ak.example.geometry
abstract sealed class Shape{
def color: String
}
abstract sealed class Ellipse extends Shape {
def a: Int
def b: Int
}
private[geometry] final case class GeneralEllipse(color: String, a: Int, b: Int) extends Ellipse{
override def productPrefix = "Ellipse"
}
final case class Circle(color: String, r: Int) extends Ellipse {
def a=r
def b=r
}
object Ellipse {
def apply(color: String, a: Int, b: Int) = a match {
case `b` => Circle(color, a)
case _ => GeneralEllipse(color, a, b)
}
def unapply(e: Ellipse) = Option(e).map(e => (e.color, e.a, e.b))
}
package com.v6ak.example.geometry
object Demo extends Application{ // use App instead of Application in Scala <= 2.9
val a = Circle("yellow", 5)
val b = Ellipse("yellow", 5, 5)
val c = Ellipse("yellow", 4, 5)
println(a + " is equal to " + b + ": " + (a == b) + " (should be true)")
println(a + " is equal to " + c + ": " + (a == c) + " (should be false)")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment