Upgrade to Pro — share decks privately, control downloads, hide ads and more …

What is a nominal type?

takasek
February 28, 2017

What is a nominal type?

The slide for the talk on:
try! Swift Tokyo 2017 RejectCon - connpass
https://rmp-quipper.connpass.com/event/49316/

Swiftで、Anyなどの一部の型にextensionを書こうとしたときのエラー文言「Non-nominal type 'Any' cannot be extended」
「Non-nominal type」とは何なのか? について掘っていたら、型システムの基本的な分類に辿り着きましたよ、というお話です。
英語および日本語の資料です。

takasek

February 28, 2017
Tweet

More Decks by takasek

Other Decks in Programming

Transcript

  1. 4

  2. 7

  3. 11

  4. 14

  5. 20

  6. 21

  7. ܕγεςϜೖ໳ −ϓϩάϥϛϯάݴޠͱܕͷཧ࿦− 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
  8. 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
  9. 4 Compound (structural) types: 4 function types 4 tuple types

    !❗ɹɹɹɹɹɹɹɹ※ Any ≒ empty set of protocols 25
  10. 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
  11. tuples as structural types typealias ItemA = ( id: Int,

    name: String ) typealias ItemB = ( id: Int, name: String ) let array: [ItemA] = [ ItemA(id: 1, name: "!"), ItemB(id: 2, name: """) //# ] 28
  12. 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
  13. Unsafety of structural types func concat(number: Int, unit: String) {

    print("\(number)\(unit)") } typealias Item = (id: Int, name: String) let onigiri = Item(id: 1, name: "!") 32
  14. Unsafety of structural types func concat(number: Int, unit: String) {

    print("\(number)\(unit)") } typealias Item = (id: Int, name: String) let onigiri = Item(id: 1, name: "!") concat(onigiri) // ⛔error! 33
  15. Unsafety of structural types func concat(number: Int, unit: String) {

    print("\(number)\(unit)") } typealias Item = (id: Int, name: String) let onigiri = Item(id: 1, name: "!") let f: ((Int, String)) -> Void = concat 34
  16. Unsafety of structural types func concat(number: Int, unit: String) {

    print("\(number)\(unit)") } typealias Item = (id: Int, name: String) let onigiri = Item(id: 1, name: "!") let f: ((Int, String)) -> Void = concat f(onigiri) // 1! 35
  17. 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
  18. 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
  19. 43