SlideShare a Scribd company logo
1 of 30
Download to read offline








“Type systems are generally formulated as collections of rules
for checking the ‘consistency’ of programs.”

Benjamin C. Pierce
is, as?, as! (as )

$ swift
Welcome to Apple Swift version 2.2 (swiftlang-703.0.18.8
clang-703.0.30).
Type :help for assistance.
1> 1 is Any
$R0: Bool = true
2> 1 is AnyObject
$R1: Bool = false
3> import Foundation
4> 1 is Any
$R2: Bool = true
5> 1 is AnyObject
$R3: Bool = true // ???
is, as?, as! (as )

$ swift
Welcome to Apple Swift version 2.2 (swiftlang-703.0.18.8
clang-703.0.30).
Type :help for assistance.
1> [1] is [Any]
$R0: Bool = true
2> [1] is [AnyObject]
repl.swift:2:5: error: 'Bool' is not convertible to ‘[AnyObject]’ // ???
[1] is [AnyObject]
~~~~^~~~~~~~~~~~~~
2> import Foundation
3> [1] is [Any]
$R3: Bool = true
4> [1] is [AnyObject]
$R4: Bool = true


nil
nil
$ ghci
GHCi, version 7.8.3: http://www.haskell.org/ghc/ :? for help
…
Prelude> let square x = x * x
Prelude> :info square
square :: Num a => a -> a
Prelude> square 10
100
expression statement
// Objective-C
NSMutableAttributedString *mutableAttributedString =
[[NSMutableAttributedString alloc] init];
// Swift
let mutableAttributedString = NSMutableAttributedString()
• 3: Int (default), IntXX, UIntXX,
Double, Float, …
• 3.14159: Double (default), Float, …
$ swift
Welcome to Apple Swift version 2.2 (swiftlang-703.0.18.8
clang-703.0.30).
Type :help for assistance.
1> let x1 = 3
x1: Int = 3 // inferred to be Int
2> let x2 = Double(3)
x2: Double = 3 // inferred to be Double
3> let x3: Double = 3
x3: Double = 3 // explicit type specified
4> let y1 = 3.14159
y1: Double = 3.1415899999999999 // inferred to be Double
5> let y2 = Float(3.14159)
y2: Float = 3.14159012 // inferred to be Float
6> let y3: Float = 3.14159
y3: Float = 3.14159012 // explicit type specified
import CoreGraphics
// func CGPointMake(_ x: CGFloat, _ y: CGFloat) -> CGPoint
let x = CGFloat(1.0 / 3) // 1 / 3 = 0(?), 1.0 / 3 = 0.333
let y = CGFloat(1.0 / 3) // 1.0 / 3: Double to CGFloat
let p = CGPointMake(x, y)
// CGFloat Swift
// “1 / 3” “CGFloat / CGFloat -> CGFloat”
// “1” “3” “CGFloat”
let p = CGPointMake(1 / 3, 1 / 3)
Swift Type System
Swift Type System

More Related Content

Viewers also liked

Viewers also liked (10)

Objective-C のキャストと Swift の型変換を比べてみる #akibaswift
Objective-C のキャストと Swift の型変換を比べてみる #akibaswiftObjective-C のキャストと Swift の型変換を比べてみる #akibaswift
Objective-C のキャストと Swift の型変換を比べてみる #akibaswift
 
バ、バカな...!ハッカソンの中で成長しているだと...!?
バ、バカな...!ハッカソンの中で成長しているだと...!?バ、バカな...!ハッカソンの中で成長しているだと...!?
バ、バカな...!ハッカソンの中で成長しているだと...!?
 
AWS Cognitoを実際のアプリで導入してハマったこと
AWS Cognitoを実際のアプリで導入してハマったことAWS Cognitoを実際のアプリで導入してハマったこと
AWS Cognitoを実際のアプリで導入してハマったこと
 
Swift で JavaScript 始めませんか? #iOSDC
Swift で JavaScript 始めませんか? #iOSDCSwift で JavaScript 始めませんか? #iOSDC
Swift で JavaScript 始めませんか? #iOSDC
 
Unowned / Weak References with Closure
Unowned / Weak References with ClosureUnowned / Weak References with Closure
Unowned / Weak References with Closure
 
iOSアプリ開発のCI環境 - Jenkins編 -
iOSアプリ開発のCI環境 - Jenkins編 -iOSアプリ開発のCI環境 - Jenkins編 -
iOSアプリ開発のCI環境 - Jenkins編 -
 
Xcodeの管理を楽に - Jenkins編 -
Xcodeの管理を楽に - Jenkins編 -Xcodeの管理を楽に - Jenkins編 -
Xcodeの管理を楽に - Jenkins編 -
 
VC「もしかして...」Model「私たち...」「「入れ替わってるー!?」」を前前前世から防ぐ方法
VC「もしかして...」Model「私たち...」「「入れ替わってるー!?」」を前前前世から防ぐ方法VC「もしかして...」Model「私たち...」「「入れ替わってるー!?」」を前前前世から防ぐ方法
VC「もしかして...」Model「私たち...」「「入れ替わってるー!?」」を前前前世から防ぐ方法
 
Blending Culture in Twitter Client
Blending Culture in Twitter ClientBlending Culture in Twitter Client
Blending Culture in Twitter Client
 
Swift らしい表現を目指そう #eventdots
Swift らしい表現を目指そう #eventdotsSwift らしい表現を目指そう #eventdots
Swift らしい表現を目指そう #eventdots
 

Similar to Swift Type System

Python-GTK
Python-GTKPython-GTK
Python-GTK
Yuren Ju
 
Python for High School Programmers
Python for High School ProgrammersPython for High School Programmers
Python for High School Programmers
Siva Arunachalam
 

Similar to Swift Type System (20)

Swift internals
Swift internalsSwift internals
Swift internals
 
Chapter 2 Python Language Basics, IPython.pptx
Chapter 2 Python Language Basics, IPython.pptxChapter 2 Python Language Basics, IPython.pptx
Chapter 2 Python Language Basics, IPython.pptx
 
Idioms in swift 2016 05c
Idioms in swift 2016 05cIdioms in swift 2016 05c
Idioms in swift 2016 05c
 
Frege - consequently functional programming for the JVM
Frege - consequently functional programming for the JVMFrege - consequently functional programming for the JVM
Frege - consequently functional programming for the JVM
 
Introduction to julia
Introduction to juliaIntroduction to julia
Introduction to julia
 
BED-Con 2016 - I have a stream - Einsichten in Reactive Programming
BED-Con 2016 - I have a stream - Einsichten in Reactive ProgrammingBED-Con 2016 - I have a stream - Einsichten in Reactive Programming
BED-Con 2016 - I have a stream - Einsichten in Reactive Programming
 
Perl세미나
Perl세미나Perl세미나
Perl세미나
 
Perl세미나
Perl세미나Perl세미나
Perl세미나
 
Kotlin
KotlinKotlin
Kotlin
 
Perl세미나
Perl세미나Perl세미나
Perl세미나
 
Python fundamentals - basic | WeiYuan
Python fundamentals - basic | WeiYuanPython fundamentals - basic | WeiYuan
Python fundamentals - basic | WeiYuan
 
Frege Tutorial at JavaOne 2015
Frege Tutorial at JavaOne 2015Frege Tutorial at JavaOne 2015
Frege Tutorial at JavaOne 2015
 
掀起 Swift 的面紗
掀起 Swift 的面紗掀起 Swift 的面紗
掀起 Swift 的面紗
 
Workhop iOS 1: Fundamentos de Swift
Workhop iOS 1: Fundamentos de SwiftWorkhop iOS 1: Fundamentos de Swift
Workhop iOS 1: Fundamentos de Swift
 
Python-GTK
Python-GTKPython-GTK
Python-GTK
 
Python for High School Programmers
Python for High School ProgrammersPython for High School Programmers
Python for High School Programmers
 
Swift - the future of iOS app development
Swift - the future of iOS app developmentSwift - the future of iOS app development
Swift - the future of iOS app development
 
Getting modern with logging via log4perl
Getting modern with logging via log4perlGetting modern with logging via log4perl
Getting modern with logging via log4perl
 
Loops in Python.pptx
Loops in Python.pptxLoops in Python.pptx
Loops in Python.pptx
 
Porting to Python 3
Porting to Python 3Porting to Python 3
Porting to Python 3
 

More from Goichi Hirakawa (9)

Strings and characters in swift
Strings and characters in swiftStrings and characters in swift
Strings and characters in swift
 
WebKit Overview
WebKit OverviewWebKit Overview
WebKit Overview
 
Server-side Swift
Server-side SwiftServer-side Swift
Server-side Swift
 
Swift API Design Guidelines (dec 3, 2015)
Swift API Design Guidelines (dec 3, 2015)Swift API Design Guidelines (dec 3, 2015)
Swift API Design Guidelines (dec 3, 2015)
 
New Objective-C Features for Swift 2.0
New Objective-C Features for Swift 2.0New Objective-C Features for Swift 2.0
New Objective-C Features for Swift 2.0
 
Objective-C Generics
Objective-C GenericsObjective-C Generics
Objective-C Generics
 
Objective-C Generics LT version
Objective-C Generics LT versionObjective-C Generics LT version
Objective-C Generics LT version
 
Mix and Match / Swift and Objective-C
Mix and Match / Swift and Objective-CMix and Match / Swift and Objective-C
Mix and Match / Swift and Objective-C
 
Strings and Characters in Swift
Strings and Characters in SwiftStrings and Characters in Swift
Strings and Characters in Swift
 

Recently uploaded

The title is not connected to what is inside
The title is not connected to what is insideThe title is not connected to what is inside
The title is not connected to what is inside
shinachiaurasa2
 
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
masabamasaba
 
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
Health
 
AI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
AI Mastery 201: Elevating Your Workflow with Advanced LLM TechniquesAI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
AI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
VictorSzoltysek
 
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
masabamasaba
 

Recently uploaded (20)

%in Soweto+277-882-255-28 abortion pills for sale in soweto
%in Soweto+277-882-255-28 abortion pills for sale in soweto%in Soweto+277-882-255-28 abortion pills for sale in soweto
%in Soweto+277-882-255-28 abortion pills for sale in soweto
 
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
 
%in Midrand+277-882-255-28 abortion pills for sale in midrand
%in Midrand+277-882-255-28 abortion pills for sale in midrand%in Midrand+277-882-255-28 abortion pills for sale in midrand
%in Midrand+277-882-255-28 abortion pills for sale in midrand
 
%in kempton park+277-882-255-28 abortion pills for sale in kempton park
%in kempton park+277-882-255-28 abortion pills for sale in kempton park %in kempton park+277-882-255-28 abortion pills for sale in kempton park
%in kempton park+277-882-255-28 abortion pills for sale in kempton park
 
The title is not connected to what is inside
The title is not connected to what is insideThe title is not connected to what is inside
The title is not connected to what is inside
 
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
 
Microsoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdfMicrosoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdf
 
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
 
Right Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsRight Money Management App For Your Financial Goals
Right Money Management App For Your Financial Goals
 
VTU technical seminar 8Th Sem on Scikit-learn
VTU technical seminar 8Th Sem on Scikit-learnVTU technical seminar 8Th Sem on Scikit-learn
VTU technical seminar 8Th Sem on Scikit-learn
 
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
 
WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...
WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...
WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...
 
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
 
WSO2CON 2024 - Does Open Source Still Matter?
WSO2CON 2024 - Does Open Source Still Matter?WSO2CON 2024 - Does Open Source Still Matter?
WSO2CON 2024 - Does Open Source Still Matter?
 
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
 
Payment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdf
Payment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdfPayment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdf
Payment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdf
 
AI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
AI Mastery 201: Elevating Your Workflow with Advanced LLM TechniquesAI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
AI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
 
Introducing Microsoft’s new Enterprise Work Management (EWM) Solution
Introducing Microsoft’s new Enterprise Work Management (EWM) SolutionIntroducing Microsoft’s new Enterprise Work Management (EWM) Solution
Introducing Microsoft’s new Enterprise Work Management (EWM) Solution
 
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
 
WSO2Con2024 - Enabling Transactional System's Exponential Growth With Simplicity
WSO2Con2024 - Enabling Transactional System's Exponential Growth With SimplicityWSO2Con2024 - Enabling Transactional System's Exponential Growth With Simplicity
WSO2Con2024 - Enabling Transactional System's Exponential Growth With Simplicity
 

Swift Type System

  • 1.
  • 2.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8. “Type systems are generally formulated as collections of rules for checking the ‘consistency’ of programs.” Benjamin C. Pierce
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14. is, as?, as! (as )
 $ swift Welcome to Apple Swift version 2.2 (swiftlang-703.0.18.8 clang-703.0.30). Type :help for assistance. 1> 1 is Any $R0: Bool = true 2> 1 is AnyObject $R1: Bool = false 3> import Foundation 4> 1 is Any $R2: Bool = true 5> 1 is AnyObject $R3: Bool = true // ???
  • 15. is, as?, as! (as )
 $ swift Welcome to Apple Swift version 2.2 (swiftlang-703.0.18.8 clang-703.0.30). Type :help for assistance. 1> [1] is [Any] $R0: Bool = true 2> [1] is [AnyObject] repl.swift:2:5: error: 'Bool' is not convertible to ‘[AnyObject]’ // ??? [1] is [AnyObject] ~~~~^~~~~~~~~~~~~~ 2> import Foundation 3> [1] is [Any] $R3: Bool = true 4> [1] is [AnyObject] $R4: Bool = true
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 22.
  • 23.
  • 24. $ ghci GHCi, version 7.8.3: http://www.haskell.org/ghc/ :? for help … Prelude> let square x = x * x Prelude> :info square square :: Num a => a -> a Prelude> square 10 100
  • 25. expression statement // Objective-C NSMutableAttributedString *mutableAttributedString = [[NSMutableAttributedString alloc] init]; // Swift let mutableAttributedString = NSMutableAttributedString()
  • 26. • 3: Int (default), IntXX, UIntXX, Double, Float, … • 3.14159: Double (default), Float, …
  • 27. $ swift Welcome to Apple Swift version 2.2 (swiftlang-703.0.18.8 clang-703.0.30). Type :help for assistance. 1> let x1 = 3 x1: Int = 3 // inferred to be Int 2> let x2 = Double(3) x2: Double = 3 // inferred to be Double 3> let x3: Double = 3 x3: Double = 3 // explicit type specified 4> let y1 = 3.14159 y1: Double = 3.1415899999999999 // inferred to be Double 5> let y2 = Float(3.14159) y2: Float = 3.14159012 // inferred to be Float 6> let y3: Float = 3.14159 y3: Float = 3.14159012 // explicit type specified
  • 28. import CoreGraphics // func CGPointMake(_ x: CGFloat, _ y: CGFloat) -> CGPoint let x = CGFloat(1.0 / 3) // 1 / 3 = 0(?), 1.0 / 3 = 0.333 let y = CGFloat(1.0 / 3) // 1.0 / 3: Double to CGFloat let p = CGPointMake(x, y) // CGFloat Swift // “1 / 3” “CGFloat / CGFloat -> CGFloat” // “1” “3” “CGFloat” let p = CGPointMake(1 / 3, 1 / 3)