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
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)) | |
} |
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
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