Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Swift Type System

3,627 views

Published on

AKIBA.swift 第2回
2016/05/30 19:00〜21:30

Published in: Software
  • Be the first to comment

Swift Type System

  1. 1. 
 
 

  2. 2.
  3. 3. “Type systems are generally formulated as collections of rules for checking the ‘consistency’ of programs.” Benjamin C. Pierce
  4. 4. 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 // ???
  5. 5. 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
  6. 6. 
 nil nil
  7. 7. $ 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
  8. 8. expression statement // Objective-C NSMutableAttributedString *mutableAttributedString = [[NSMutableAttributedString alloc] init]; // Swift let mutableAttributedString = NSMutableAttributedString()
  9. 9. • 3: Int (default), IntXX, UIntXX, Double, Float, … • 3.14159: Double (default), Float, …
  10. 10. $ 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
  11. 11. 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)

×