w λϓϧΛආ͚Δͦͷࣈʹҙຯ͋Δ͔ʁ w 4DBMBͳྫ֎ॲཧޭɾࣦഊΛͱͯ͠දݱ͢Δ w DBTFDMBTTʹ੍Λ࣋ͨͤΔSFRVJSFͷར༻ w TFBMFEUSBJUͰཏੑΛ୲อ͢ΔίϯύΠϥʹ͓ئ͍ ˠ 4 D B M B Λ ֶ ͼ ࢝ Ί ͨ ࠒ ͷ ࣗ ʹ ɹ ڭ ͑ͯ ͋ ͛ ͨ ͍ ༰
user = User("Martin", Place("Tokyo", "Shibuya")) 2 3 user match { 4 case User(name, Place(pref, _)) => "$name in $pref" 5 case _ => 6 } 7 // => Martin lives in Tokyo!
Int = x match { 2 case x:String => x.length * 2 3 case x: Int => x * 2 4 case _ => -1 5 } 6 7 count2x("scala") // 10 8 count2x(7) // 14 ☺ ίʔυͷݟ௨্͕͠ɻ ΩϟετෆཁͰ҆৺ɻ
String = x match { 2 case x: List[Int] => "This is List of Int!" 3 case _ => "What?" 4 } 5 6 detectType(List("string!")) // => This is List of Int! +BWBͱಉ༷ɺίϨΫγϣϯͷཁૉܕͷใ ࣮ߦ࣌ʹཧ͞Εͳ͍ ˞"SSBZΛআ͘
4 ) { 5 require(age >= 0, "Age must be natural number!") 6 } ੍ Λ ͨ ͤ ͨ D B T F D M B T T 1 val joe = Person("Joe Cool", -2)) 2 // => java.lang.IllegalArgumentException: requirement failed: Age must be natural number!
4 ) { 5 require(age >= 0, "Age must be natural number!") 6 } ੍ Λ ͨ ͤ ͨ D B T F D M B T T &JUIFS5SZͱΈ߹ΘͤΔͱ͏Ε͍͠ ☺ 1 val joe = Person("Joe Cool", -2)) 2 // => java.lang.IllegalArgumentException: requirement failed: Age must be natural number!
Ϛ ο ν 1 case class User(name: String, age: Int) 2 3 trait Plan 4 case class Free(user: User) extends Plan // ແྉϓϥϯ 5 case class Premium(user: User) extends Plan // ϓϨϛΞϜϓϥϯ
Ϛ ο ν 1 case class User(name: String, age: Int) 2 3 trait Plan 4 case class Free(user: User) extends Plan // ແྉϓϥϯ 5 case class Premium(user: User) extends Plan // ϓϨϛΞϜϓϥϯ 6 7 val userA = Free(User("Taro", 23)) 8 val userB = Premium(User("Jiro", 25)) 9
Ϛ ο ν 1 case class User(name: String, age: Int) 2 3 trait Plan 4 case class Free(user: User) extends Plan // ແྉϓϥϯ 5 case class Premium(user: User) extends Plan // ϓϨϛΞϜϓϥϯ 6 7 val userA = Free(User("Taro", 23)) 8 val userB = Premium(User("Jiro", 25)) 9 10 userB match { 11 case Free(User(name, _)) => println(s"Hi, $name.") 12 case Premium(User(name, _)) => println(s"Hello, $name!") 13 }
Ϛ ο ν 1 case class User(name: String, age: Int) 2 3 trait Plan 4 case class Free(user: User) extends Plan // ແྉϓϥϯ 5 case class Premium(user: User) extends Plan // ϓϨϛΞϜϓϥϯ 6 7 val userA = Free(User("Taro", 23)) 8 val userB = Premium(User("Jiro", 25)) 9 10 userB match { 11 case Free(User(name, _)) => println(s"Hi, $name.") 12 case Premium(User(name, _)) => println(s"Hello, $name!") 13 } 14 // => Hello, Jiro!
User(name: String, age: Int) 2 3 trait Plan 4 case class Free(user: User) extends Plan 5 case class Premium(user: User) extends Plan 6 case class Campaign(user: User) extends Plan 7 8 val userA = Free(User("Taro", 23)) 9 val userB = Premium(User("Jiro", 25)) 10 val userC = Campaign(User("Saburo", 28))
User(name: String, age: Int) 2 3 trait Plan 4 case class Free(user: User) extends Plan 5 case class Premium(user: User) extends Plan 6 case class Campaign(user: User) extends Plan 7 8 val userA = Free(User("Taro", 23)) 9 val userB = Premium(User("Jiro", 25)) 10 val userC = Campaign(User("Saburo", 28)) 11 12 Seq(userA, userB, userC) foreach { 13 case Free(User(name, _)) => println(s"Hi, $name.") 14 case Premium(User(name, _)) => println(s"Hello, $name!") 15 }
ཏ ੑ Λ ୲ อ ͢ Δ 1 case class User(name: String, age: Int) 2 3 sealed trait Plan 4 case class Free(user: User) extends Plan 5 case class Premium(user: User) extends Plan 6 case class Campaign(user: User) extends Plan 7 8 val userA = Free(User("Taro", 23)) 9 val userB = Premium(User("Jiro", 25)) 10 val userC = Campaign(User("Saburo", 28)) 11 12 Seq(userA, userB, userC) foreach { 13 case Free(User(name, _)) => println(s"Hi, $name.") 14 case Premium(User(name, _)) => println(s"Hello, $name!") 15 }
Seq(userA, userB, userC) foreach { [warn] ^ [warn] one warning found 4 F B M F E म ০ ࢠ Ͱ ཏ ੑ Λ ୲ อ ͢ Δ ίϯύΠϧ͢Δͱʜ ʮ$BNQBJHO͕དྷͨΒམͪΔͰʂʯͱ ίϯύΠϥ͕ڭ͑ͯ͘ΕΔ ☺
Seq(userA, userB, userC) foreach { [warn] ^ [warn] one warning found 4 F B M F E म ০ ࢠ Ͱ ཏ ੑ Λ ୲ อ ͢ Δ ίϯύΠϧ͢Δͱʜ ʮ$BNQBJHO͕དྷͨΒམͪΔͰʂʯͱ ίϯύΠϥ͕ڭ͑ͯ͘ΕΔ ☺ 8BSOJOHແࢹ͠ͳ͍ͰͶ ☺