Created
October 21, 2013 13:08
-
-
Save Arakaki/7083598 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
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