38
Pattern match with case class 佐々木 (Sasaki Kai) 2Swift勉強会 @TechBuzzSpace

Pattern match with case class

Embed Size (px)

Citation preview

Page 1: Pattern match with case class

Pattern match with case class

佐々木 海(Sasaki Kai) 第2回Swift勉強会

@TechBuzzSpace

Page 2: Pattern match with case class

Who am I?佐々木 海(Sasaki Kai)

@Lewuathe

プラットフォームエンジニア

emailとか、Push通知とか

C/C++, nodejs, Python

iOSアプリは普段作ってません

Page 3: Pattern match with case class

Topic

ケースクラスを使ったパターンマッチを Swiftで実装してみた話

Page 4: Pattern match with case class

What is case class?Scalaに組み込みの仕組み

!

!

クラス名の前にcaseキーワード

abstract class Expr case class Var(name: String) extends Expr case class Number(num: Double) extends Expr

Page 5: Pattern match with case class

Why case class?ファクトリメソッドの生成

Page 6: Pattern match with case class

Why case class?ファクトリメソッドの生成

constant memberの生成

Page 7: Pattern match with case class

Why case class?ファクトリメソッドの生成

constant memberの生成

toString,equalsとかの実装

Page 8: Pattern match with case class

Why case class?ファクトリメソッドの生成

constant memberの生成

toString,equalsとかの実装

コンストラクタパターン

Page 9: Pattern match with case class

What is constructor pattern?

case class User(name: String, age: Int){} !!!

Page 10: Pattern match with case class

What is constructor pattern?

case class User(name: String, age: Int){} !val you = User(“Nobita”, 12) !!!

Page 11: Pattern match with case class

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?” } !

Page 12: Pattern match with case class

What is constructor pattern?要するに

!

コンストラクタに渡された引数リストで パターンマッチを行う仕組み

Page 13: Pattern match with case class

BTW, Swift?Swiftではパターンマッチが使える

switch文

loop

変数binding

tuple, identifier

Page 14: Pattern match with case class

BTW, Swift?

でもコンストラクタパターンは使えない

Page 15: Pattern match with case class

SwiftCaseコンストラクタパターンによるマッチングをするためのライブラリ

https://github.com/Lewuathe/SwiftCase

Page 16: Pattern match with case class

How to useclass User: SwiftCase { let name: String let age: Int ! init(name: String, age: Int) { self.name = name self.age = age super.init(name, age) } } !

Page 17: Pattern match with case class

How to useclass User: SwiftCase { let name: String let age: Int ! init(name: String, age: Int) { self.name = name self.age = age super.init(name, age) } } !

SwiftCaseを継承させる

Page 18: Pattern match with case class

How to useclass 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に

パラメタリストを渡す

Page 19: Pattern match with case class

How to uselet 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 !

Page 20: Pattern match with case class

How to uselet 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

直書き

Page 21: Pattern match with case class

Recursive matchclass UserPair: SwiftCase { let first: User let second: User ! init(first: User, second: User) { self.first = first self.second = second super.init(first, second) } } !

メンバにUser型

Page 22: Pattern match with case class

Recursive matchclass 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に

パラメタリストを渡す

Page 23: Pattern match with case class

Recursive matchlet nobi = User(name: “Nobita”, 12) let take = User(name: “Takeshi”, 13) !!let userPair = UserPair(first: nobi, second: take) !!

Page 24: Pattern match with case class

Recursive matchswitch 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?”) } !

Page 25: Pattern match with case class

Recursive matchswitch 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?”) } !

Page 26: Pattern match with case class

Recursive matchswitch 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

Page 27: Pattern match with case class

Why SwiftCase?

コンストラクタパターンによるマッチング

SwiftCase同士での再帰的なマッチング

Page 28: Pattern match with case class

TipsExpression pattern

Type casting pattern

Equitable Protocol

Variadic Parameters

Page 29: Pattern match with case class

Expression Patternswitch文のパターンマッチで呼ばれる

func ~= (c1: SwiftCase, c2: SwiftCase)-> Bool { return internalMathcing(c1, c2) }

Page 30: Pattern match with case class

Typecasting Patternある型としてその変数が利用できるかを判定できる

let m = matchArr[i] as? SwiftCase

optional typeとしてcastした結果を返してくれる

Page 31: Pattern match with case class

Equitable Protocol== 演算子を定義している

今回matchingに==を使っている

Page 32: Pattern match with case class

Equitable Protocol== 演算子を定義している

今回matchingに==を使っている

NSObjectのサブクラスに対して

Page 33: Pattern match with case class

Equitable Protocol== 演算子を定義している

今回matchingに==を使っている

NSObjectのサブクラスに対して

AnyはEquitableでない

Page 34: Pattern match with case class

Variadic Parameters可変長引数

init(_ params: NSObject...) { // Do something }

NSObjectのコレクション型

Page 35: Pattern match with case class

Variadic Parameters可変長引数

init(_ params: NSObject...) { // Do something }

NSObjectのコレクション型

名前付きパラメタを要求しない

Page 36: Pattern match with case class

In Conclusionコンストラクタパターンは便利

Advanced Swiftの内容がよくわかった

できれば変数束縛も実装したい(たぶんできない)

Page 37: Pattern match with case class

ReferenceThe 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

Page 38: Pattern match with case class

Thank you