Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Refinement types
Search
hectr
March 21, 2019
Programming
0
210
Refinement types
Refinement types for Swift
hectr
March 21, 2019
Tweet
Share
Other Decks in Programming
See All in Programming
テストコードはもう書かない:JetBrains AI Assistantに委ねる非同期処理のテスト自動設計・生成
makun
0
530
為你自己學 Python - 冷知識篇
eddie
1
350
Oracle Database Technology Night 92 Database Connection control FAN-AC
oracle4engineer
PRO
1
470
さようなら Date。 ようこそTemporal! 3年間先行利用して得られた知見の共有
8beeeaaat
3
1.5k
複雑なフォームに立ち向かう Next.js の技術選定
macchiitaka
2
220
Navigation 2 を 3 に移行する(予定)ためにやったこと
yokomii
0
340
@Environment(\.keyPath)那么好我不允许你们不知道! / atEnvironment keyPath is so good and you should know it!
lovee
0
130
ぬるぬる動かせ! Riveでアニメーション実装🐾
kno3a87
1
230
実用的なGOCACHEPROG実装をするために / golang.tokyo #40
mazrean
1
290
アプリの "かわいい" を支えるアニメーションツールRiveについて
uetyo
0
280
AI時代のUIはどこへ行く?
yusukebe
18
9.1k
Azure SRE Agentで運用は楽になるのか?
kkamegawa
0
2.5k
Featured
See All Featured
The Language of Interfaces
destraynor
161
25k
Navigating Team Friction
lara
189
15k
Fantastic passwords and where to find them - at NoRuKo
philnash
52
3.4k
Responsive Adventures: Dirty Tricks From The Dark Corners of Front-End
smashingmag
252
21k
Large-scale JavaScript Application Architecture
addyosmani
513
110k
I Don’t Have Time: Getting Over the Fear to Launch Your Podcast
jcasabona
33
2.4k
The World Runs on Bad Software
bkeepers
PRO
70
11k
ピンチをチャンスに:未来をつくるプロダクトロードマップ #pmconf2020
aki_iinuma
127
53k
Fashionably flexible responsive web design (full day workshop)
malarkey
407
66k
Sharpening the Axe: The Primacy of Toolmaking
bcantrill
44
2.5k
Building Flexible Design Systems
yeseniaperezcruz
329
39k
Building Applications with DynamoDB
mza
96
6.6k
Transcript
Refinement types github.com/hectr
a refinement type is a type endowed with a predicate
which is assumed to hold for any element of the refined type. — Wikipedia github.com/hectr
refinement types a.k.a encoding valida,ons at the typelevel github.com/hectr
Why? github.com/hectr
Stringly typed example struct Foo: Codable { let id: String
} • id cannot be nil ! • id could be empty " github.com/hectr
Refined string example struct Foo: Codable { let id: String.NonEmpty
} • id cannot be nil ! • id cannot be empty " github.com/hectr
Refined string example struct Foo: Codable { let id: String.NonEmpty
} • id cannot be nil ! • id cannot be empty " ... we'll explain this later. github.com/hectr
Almost any type is suscep0ble to be refined: • Int
(e.g. divisible by...) • Float (greater than...) • Double, etc.: (e.g. nega7ve) • String: (e.g. starts with...) • Array (e.g. contains...) • Set (e.g. count is...) github.com/hectr
The idea is not new: make illegal states unrepresentable github.com/hectr
Refinement types implementa.ons: (frameworks or dialects) • Scala • Haskell
• TypeScript • Perl • ... github.com/hectr
What about Swi,? github.com/hectr
There isn't language support for refinement types. ! github.com/hectr
There isn't language support for refinement types (yet). ! github.com/hectr
But... github.com/hectr
refined type = base type + predicate github.com/hectr
refined type ≈ wrapper type + generic constraint github.com/hectr
wrapper type + generic constraint struct Refined<Constraint: Predicate> { let
wrapped: Constraint.Value init?(_ value: Constraint.Value) { guard Constraint.isValid(value: value) else { return nil } self.wrapped = value } } github.com/hectr
wrapper type + generic constraint protocol Predicate { associatedtype Value
static func isValid(value: Value) -> Bool } github.com/hectr
Refined string example (cont) struct Not<P: Predicate>: Predicate { static
func isValid(value: P.Value) -> Bool { return !P.isValid(value: value) } } struct IsEmpty: Predicate { static func isValid(value: String) -> Bool { return value.isEmpty } } github.com/hectr
Refined string example (cont) let foo: Refined<Not<IsEmpty>> github.com/hectr
Refined string example (cont) let foo: Refined<Not<IsEmpty>> ! github.com/hectr
Refined string example (cont) // let foo: Refined<Not<IsEmpty>> extension String
{ typealias Empty = Refined<IsEmpty> typealias NonEmpty = Refined<Not<IsEmpty>> } github.com/hectr
Refined string example (cont) // let foo: Refined<Not<IsEmpty>> extension String
{ typealias Empty = Refined<IsEmpty> typealias NonEmpty = Refined<Not<IsEmpty>> } let foo: String.NonEmpty github.com/hectr
Is it that easy? github.com/hectr
No, it is not... github.com/hectr
Refined types need to seamlessly integrate with their base types.
github.com/hectr
type transparency github.com/hectr
type transparency struct Photo: Codable { var title: Refined<Count<String, LessThan<Int_20>>>
var height: Refined<RangeOf<Double_100, Double_1000>> var width: Refined<RangeOf<Double_100, Double_1000>> } let jsonString = "{\"width\":150,\"height\":150,\"title\":\"Apple\"}" let jsonData = jsonString.data(using: .utf8)! let photo = try! JSONDecoder().decode(Photo.self, from: jsonData) _ = photo.width * 2.0 _ = "photo called " + photo.title XCTAssertEqual(photo.title, "Apple") XCTAssertEqual(photo.width, 150) github.com/hectr
But that is another story... github.com/hectr
github.com/hectr
github.com/hectr
github.com/hectr
github.com/refined-swi4 github.com/hectr