Skip to content

Instantly share code, notes, and snippets.

@Arakaki
Created October 21, 2013 13:08
Show Gist options
  • Save Arakaki/7083598 to your computer and use it in GitHub Desktop.
Save Arakaki/7083598 to your computer and use it in GitHub Desktop.
object Main {
def main(args:Array[String]) {
//easy interfaces / mixins
trait Namable { val name:String; def greet:String = s"Hi $name!"}
//lazy evaluation
trait Randomable { lazy val rand:Int = (math.random * 100).toInt }
//easy class definition, support for mixins using traits
case class Person(name:String, favoriteLanguage:String) extends Namable with Randomable
case class Superhero(name:String) extends Namable with Randomable
//type aliases
type NamRam = Namable with Randomable
val person:NamRam = Person("Alice", "Scala")
//val person:Namable = Superhero("man")
//Pattern Matching
person match {
case p @ Person(n, fl) => println(s"${p.greet} p.s. we like $fl too!" +
s"Random number: ${p.rand}. (Still ${p.rand})")
case sh @ Superhero(n) => println(sh.greet, sh.rand)
case _ => println("hm...")
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment