Skip to content

Instantly share code, notes, and snippets.

@v6ak v6ak/classes.scala
Created Sep 5, 2011

Embed
What would you like to do?
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
You can’t perform that action at this time.