4. What is case class?
Scalaに組み込みの仕組み
!
!
クラス名の前にcaseキーワード
abstract class Expr
case class Var(name: String) extends Expr
case class Number(num: Double) extends Expr
11. What is constructor
pattern?
case class User(name: String, age: Int){}
!
val you = User(“Nobita”, 12)
!
you match {
case User(“Takeshi”, 13) => “He is Takeshi”
case User(“Nobita”, 12) => “He is Nobita”
case _ => “Who is he?”
}
!
16. How to use
class User: SwiftCase {
let name: String
let age: Int
!
init(name: String, age: Int) {
self.name = name
self.age = age
super.init(name, age)
}
}
!
17. How to use
class User: SwiftCase {
let name: String
let age: Int
!
init(name: String, age: Int) {
self.name = name
self.age = age
super.init(name, age)
}
}
!
SwiftCaseを
継承させる
18. How to use
class User: SwiftCase {
let name: String
let age: Int
!
init(name: String, age: Int) {
self.name = name
self.age = age
super.init(name, age)
}
}
!
SwiftCaseを
継承させる
superクラスの
initializerに
パラメタリストを渡す
19. How to use
let user = User(name: “Nobita”, 12)
!
switch user {
case User(name: “Takeshi”, age: 13):
println(“He is Takeshi”)
case User(name: “Nobita”, age: 12):
println(“He is Nobita”)
case default:
println(“Who is he?”)
}
!
// -> He is Nobita
!
20. How to use
let user = User(name: “Nobita”, 12)
!
switch user {
case User(name: “Takeshi”, age: 13):
println(“He is Takeshi”)
case User(name: “Nobita”, age: 12):
println(“He is Nobita”)
case default:
println(“Who is he?”)
}
!
// -> He is Nobita
直書き
21. Recursive match
class UserPair: SwiftCase {
let first: User
let second: User
!
init(first: User, second: User) {
self.first = first
self.second = second
super.init(first, second)
}
}
!
メンバにUser型
22. Recursive match
class UserPair: SwiftCase {
let first: User
let second: User
!
init(first: User, second: User) {
self.first = first
self.second = second
super.init(first, second)
}
}
!
メンバにUser型
superクラスの
initializerに
パラメタリストを渡す
23. Recursive match
let nobi = User(name: “Nobita”, 12)
let take = User(name: “Takeshi”, 13)
!
!
let userPair = UserPair(first: nobi,
second: take)
!
!
24. Recursive match
switch userPair {
case User(“Takeshi”, 13):
println(“He is Takeshi”)
case UserPair(User(name: “Nobita”, age: 100),
User(name: “Takeshi”, age: 99)):
println(“They are too old”)
case UserPair(User(name: “Nobita”, age: 12),
User(name: “Takeshi”, age: 13)):
println(“Yes, that’s right”)
case default:
println(“Who are they?”)
}
!
25. Recursive match
switch userPair {
case User(“Takeshi”, 13):
println(“He is Takeshi”)
case UserPair(User(name: “Nobita”, age: 100),
User(name: “Takeshi”, age: 99)):
println(“They are too old”)
case UserPair(User(name: “Nobita”, age: 12),
User(name: “Takeshi”, age: 13)):
println(“Yes, that’s right”)
case default:
println(“Who are they?”)
}
!
26. Recursive match
switch userPair {
case User(“Takeshi”, 13):
println(“He is Takeshi”)
case UserPair(User(name: “Nobita”, age: 100),
User(name: “Takeshi”, age: 99)):
println(“They are too old”)
case UserPair(User(name: “Nobita”, age: 12),
User(name: “Takeshi”, age: 13)):
println(“Yes, that’s right”)
case default:
println(“Who are they?”)
}
!
メンバのメンバ
とmatch
37. Reference
The Swift programming language
https://developer.apple.com/library/prerelease/
ios/documentation/Swift/Conceptual/
Swift_Programming_Language/index.html#//
apple_ref/doc/uid/TP40014097
Advanced Swift
https://developer.apple.com/videos/wwdc/2014/?
id=404