What is a nominal type?

704056da9a4c4e075ad14479beaebab7?s=47 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」とは何なのか? について掘っていたら、型システムの基本的な分類に辿り着きましたよ、というお話です。
英語および日本語の資料です。

704056da9a4c4e075ad14479beaebab7?s=128

takasek

February 28, 2017
Tweet

Transcript

  1. 4.

    4

  2. 7.

    7

  3. 11.

    11

  4. 12.
  5. 14.

    14

  6. 15.
  7. 16.
  8. 20.

    20

  9. 21.

    21

  10. 22.

    ܕγεςϜೖ໳ −ϓϩάϥϛϯάݴޠͱܕͷཧ࿦− 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
  11. 24.

    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
  12. 25.

    4 Compound (structural) types: 4 function types 4 tuple types

    !❗ɹɹɹɹɹɹɹɹ※ Any ≒ empty set of protocols 25
  13. 27.

    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
  14. 28.

    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
  15. 29.

    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
  16. 32.

    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
  17. 33.

    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
  18. 34.

    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
  19. 35.

    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
  20. 37.
  21. 38.

    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
  22. 39.

    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
  23. 43.

    43