What is a nominal type?
by takasek
2017/2/28 try! Swift RejectCon
1
Slide 2
Slide 2 text
I ❤
extension
2
Slide 3
Slide 3 text
I ❤
to extend
everything
3
Slide 4
Slide 4 text
4
Slide 5
Slide 5 text
Int says
hello!!
5
Slide 6
Slide 6 text
Everyone,
say hello!!
6
Slide 7
Slide 7 text
7
Slide 8
Slide 8 text
UIViewControllerʮ!ʯ
NSStringʮ!ʯ
NSErrorʮ!ʯ
8
Slide 9
Slide 9 text
Anyone else?!
9
Slide 10
Slide 10 text
Any-san kochira!
10
Slide 11
Slide 11 text
11
Slide 12
Slide 12 text
!"
12
Slide 13
Slide 13 text
How about you guys,
closure-san?!
tuple-san?!
13
Slide 14
Slide 14 text
14
Slide 15
Slide 15 text
!"
15
Slide 16
Slide 16 text
!
16
Slide 17
Slide 17 text
❓❔❓❔❓❔❓❔❓
Non-nominal type
❔❓❔❓❔❓❔❓❔
17
Slide 18
Slide 18 text
The Swift Programming Language
(Swift 3.0.1): Types
https://developer.apple.com/library/content/documentation/Swift/
Conceptual/SwiftProgrammingLanguage/Types.html
" In Swift, there are two kinds of types:
named types
and compound types. "
18
Slide 19
Slide 19 text
Named type
== Nominal type ?
Compound type
== Non-nominal type ?
19
Slide 20
Slide 20 text
20
Slide 21
Slide 21 text
21
Slide 22
Slide 22 text
ܕγεςϜೖ
−ϓϩάϥϛϯάݴޠͱܕͷཧ−
Types and Programming Languages (MIT Press)
4 JavaͷΑ͏ʹɺ໊લ͕ॏཁͰɺ෦ܕ͕ؔ໌ࣔతʹએݴ͞ΕΔܕ
γεςϜΛ໊લతͱ͍͏ɻ
(…)໊લ͕ຊ࣭తͰͳ͘ɺ෦ܕ͕ؔܕͷߏͷ্ʹఆٛ͞
ΕΔܕγεςϜΛߏతͱ͍͏ɻ (P197)
4 Type systems like Java's, in which names are significant and
subtyping is explicitly declared, are alled nominal.
Type systems (...) in which names are inessential and
subtyping is defined directly on the structures of types are
called structural. (P252)
22
Slide 23
Slide 23 text
Named type
== Nominal type !
Compound type
== Non-nominal type
== Structural type !
23
Slide 24
Slide 24 text
a named type / a compound type
4 Named types:
4 classes
4 structures
4 enumerations
4 protocols
4 Compound types:
4 function types
4 tuple types
24
Slide 25
Slide 25 text
4 Compound (structural) types:
4 function types
4 tuple types
!❗ɹɹɹɹɹɹɹɹ※ Any ≒ empty set of protocols
25
Slide 26
Slide 26 text
Structural type
vs
Nominal type
26
Slide 27
Slide 27 text
structs as nominal types
struct ItemA {
let id: Int
let name: String
}
struct ItemB {
let id: Int
let name: String
}
let array: [ItemA] = [
ItemA(id: 1, name: "!"),
ItemB(id: 2, name: """) as ItemA //#
]
27
Nominal type vs Structural type
4 structural type
4 ⭕ flexiblity
4 permits the creation of ad hoc types and protocols.
4
https://en.wikipedia.org/wiki/Structural_type_system
4 nominal type
4 ⭕ better type-safety
4 useful not only during typechecking, but at run time as well.
(...) e.g., Java's instanceOf test and downcasting operation.
4 Types and Programming Languages P252
29
Why Non-nominal type cannot be
extended?
Because...
Nominal typing is useful at preventing
"accidental" type equivalence!
Nominal type system - Wikipedia
https://en.wikipedia.org/wiki/Nominal_type_system
38
Slide 39
Slide 39 text
The Swift Programming Language
(Swift 3.0.1): The Basics
https://developer.apple.com/library/content/documentation/Swift/Conceptual/
SwiftProgrammingLanguage/TheBasics.html
" Swift is a type-safe language.
ɹA type safe language encourages you
ɹto be clear about the types of values
ɹyour code can work with. "
39
Slide 40
Slide 40 text
I ❤
Swift
!
40
Slide 41
Slide 41 text
༨ஊ
digression
41
Slide 42
Slide 42 text
Swift
effectively uses
structural types' flexibility
under the core.
42