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
Sponsored
·
Your Podcast. Everywhere. Effortlessly.
Share. Educate. Inspire. Entertain. You do you. We'll handle the rest.
→
hectr
March 21, 2019
Programming
0
220
Refinement types
Refinement types for Swift
hectr
March 21, 2019
Tweet
Share
Other Decks in Programming
See All in Programming
Strategy for Finding a Problem for OSS: With Real Examples
kibitan
0
120
メッセージングを利用して時間的結合を分離しよう #phperkaigi
kajitack
3
490
脱 雰囲気実装!AgentCoreを良い感じにWEBアプリケーションに組み込むために
takuyay0ne
3
410
AI時代の脳疲弊と向き合う ~言語学としてのPHP~
sakuraikotone
1
1.6k
20260313 - Grafana & Friends Taipei #1 - Kubernetes v1.36 的開發雜記:那些困在 Alpha 加護病房太久的 Metrics
tico88612
0
240
Geminiをパートナーに神社DXシステムを個人開発した話(いなめぐDX 開発振り返り)
fujiba
0
120
Claude Code Skill入門
mayahoney
0
440
Codex の「自走力」を高める
yorifuji
0
1.3k
AIと共にエンジニアとPMの “二刀流”を実現する
naruogram
0
100
PHPで TLSのプロトコルを実装してみる
higaki_program
0
550
RailsのValidatesをSwift Macrosで再現してみた
hokuron
0
140
2026-03-27 #terminalnight 変数展開とコマンド展開でターミナル作業をスマートにする方法
masasuzu
0
240
Featured
See All Featured
Build your cross-platform service in a week with App Engine
jlugia
234
18k
ラッコキーワード サービス紹介資料
rakko
1
2.8M
Future Trends and Review - Lecture 12 - Web Technologies (1019888BNR)
signer
PRO
0
3.3k
A Guide to Academic Writing Using Generative AI - A Workshop
ks91
PRO
0
250
Raft: Consensus for Rubyists
vanstee
141
7.4k
技術選定の審美眼(2025年版) / Understanding the Spiral of Technologies 2025 edition
twada
PRO
118
110k
Designing Powerful Visuals for Engaging Learning
tmiket
1
300
16th Malabo Montpellier Forum Presentation
akademiya2063
PRO
0
83
Being A Developer After 40
akosma
91
590k
Refactoring Trust on Your Teams (GOTO; Chicago 2020)
rmw
35
3.4k
Marketing Yourself as an Engineer | Alaka | Gurzu
gurzu
0
170
We Are The Robots
honzajavorek
0
200
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