Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Secret Swift tour
Search
Yuka Ezura
March 01, 2018
Programming
7
2.8k
Secret Swift tour
2018/3/1
try! Swift Conference
Yuka Ezura
March 01, 2018
Tweet
Share
More Decks by Yuka Ezura
See All by Yuka Ezura
Mastering share sheet preview
ezura
4
1.3k
SF Symbols
ezura
9
1.2k
気遣いの iOS プログラミング
ezura
11
3.3k
Swift Album ver.4.1
ezura
3
1.4k
Swift と Kotlin
ezura
4
2.6k
哲学とSwiftの発表
ezura
5
580
アプリが動いたその先へ (iOS アプリ開発編)
ezura
1
950
Blending Kotlin's culture into Swift
ezura
1
1.2k
Re: エラーと警告でコードをデザインする
ezura
2
940
Other Decks in Programming
See All in Programming
Contemporary Test Cases
maaretp
0
130
Make Impossible States Impossibleを 意識してReactのPropsを設計しよう
ikumatadokoro
0
170
とにかくAWS GameDay!AWSは世界の共通言語! / Anyway, AWS GameDay! AWS is the world's lingua franca!
seike460
PRO
1
860
Generative AI Use Cases JP (略称:GenU)奮闘記
hideg
1
290
Enabling DevOps and Team Topologies Through Architecture: Architecting for Fast Flow
cer
PRO
0
310
.NET のための通信フレームワーク MagicOnion 入門 / Introduction to MagicOnion
mayuki
1
1.4k
Outline View in SwiftUI
1024jp
1
320
レガシーシステムにどう立ち向かうか 複雑さと理想と現実/vs-legacy
suzukihoge
14
2.2k
OnlineTestConf: Test Automation Friend or Foe
maaretp
0
100
エンジニアとして関わる要件と仕様(公開用)
murabayashi
0
280
最新TCAキャッチアップ
0si43
0
140
ヤプリ新卒SREの オンボーディング
masaki12
0
130
Featured
See All Featured
Testing 201, or: Great Expectations
jmmastey
38
7.1k
The Psychology of Web Performance [Beyond Tellerrand 2023]
tammyeverts
44
2.2k
Art, The Web, and Tiny UX
lynnandtonic
297
20k
Become a Pro
speakerdeck
PRO
25
5k
Performance Is Good for Brains [We Love Speed 2024]
tammyeverts
6
410
個人開発の失敗を避けるイケてる考え方 / tips for indie hackers
panda_program
93
16k
Build The Right Thing And Hit Your Dates
maggiecrowley
33
2.4k
"I'm Feeling Lucky" - Building Great Search Experiences for Today's Users (#IAC19)
danielanewman
226
22k
How to Create Impact in a Changing Tech Landscape [PerfNow 2023]
tammyeverts
47
2.1k
Adopting Sorbet at Scale
ufuk
73
9.1k
GitHub's CSS Performance
jonrohan
1030
460k
Exploring the Power of Turbo Streams & Action Cable | RailsConf2023
kevinliebholz
27
4.3k
Transcript
Secret Swift tour 2018/3/1 try! Swift Conference Yuka Ezura
var myVariable = 42 myVariable = 50
let r = ( ) Void var myVariable = 42
myVariable = 50
obj?.myVariable = 50
(obj?.myVariable = 50) Optional<Void>
(obj?.myVariable = 50) .map { /* do something */ }
! error: Value of optional type 'Int?' not unwrapped; obj?.myVariable
+ 50
infix operator ⭐ func ⭐ (left: Int, right: Int) ->
Int { return left + right }aaaaaaa
! error: Value of optional type 'Int?' not unwrapped; obj?.myVariable
⭐ 5 infix operator ⭐ func ⭐ (left: Int, right: Int) -> Int { return left + right }aaaaaaa
infix operator ⭐ func ⭐ (left: Int, right: Int) ->
Int { return left + right }aaaaaaa
precedencegroup FoldedIntoOptionalChaining { assignment: true } :FoldedIntoOptionalChaining infix operator ⭐
func ⭐ (left: Int, right: Int) -> Int { return left + right }aaaaaaa
obj?.myVariable ⭐ 5 // => Optional<Int> 1 ⭐ 5 //
=> Int
let myVariable: Int = /* ?? */
let myVariable: Int = { while(true) {} }()
! error: Cannot convert value of type 'Never' to specified
type 'Int' let naver: Never = unsafeBitCast((), to: Never.self) let myVariable: Int = naver
let myVariable: Int = { while(true) {} }()
(source_file (top_level_code_decl (brace_stmt (pattern_binding_decl (pattern_typed type='Int' (pattern_named type='Int' 'myVariable') (type_ident
(component id='Int' bind=Swift.(file).Int))) (call_expr type='Int' location=sample.swift:1:23 range=[sample.swift:1:23 - line:3:3] nothrow arg_labels= (closure_expr type='() -> Int' location=sample.swift:1:23 range=[sample.swift:1:23 - line:3:1] discriminator=0 (parameter_list) (brace_stmt (while_stmt (call_expr implicit type='Int1' location=sample.swift:2:10 range=[sample.swift:2:10 - line:2:15] nothrow arg_labels= (dot_syntax_call_expr implicit type='() -> Int1' location=sample.swift:2:10 range=[sample.swift:2:10 - line:2:15] nothrow (declref_expr implicit type='(Bool) -> () -> Int1' location=sample.swift:2:11 range=[sample.swift: 2:11 - line:2:11] decl=Swift.(file).Bool._getBuiltinLogicValue() function_ref=double) (paren_expr type='(Bool)' location=sample.swift:2:11 range=[sample.swift:2:10 - line:2:15] (call_expr implicit type='Bool' location=sample.swift:2:11 range=[sample.swift:2:11 - line:2:11] nothrow arg_labels=_builtinBooleanLiteral: (constructor_ref_call_expr implicit type='(Int1) -> Bool' location=sample.swift:2:11 range=[sample.swift:2:11 - line:2:11] nothrow (declref_expr implicit type='(Bool.Type) -> (Int1) -> Bool' location=sample.swift:2:11 range=[sample.swift:2:11 - line:2:11] decl=Swift.(file).Bool.init(_builtinBooleanLiteral:) function_ref=single) (type_expr implicit type='Bool.Type' location=sample.swift:2:11 range=[sample.swift:2:11 - line:2:11] typerepr='Bool')) (tuple_expr implicit type='(_builtinBooleanLiteral: Builtin.Int1)' location=sample.swift:2:11 range=[sample.swift:2:11 - line:2:11] names=_builtinBooleanLiteral (boolean_literal_expr type='Builtin.Int1' location=sample.swift:2:11 range=[sample.swift:2:11 - line:2:11] value=true))))) (tuple_expr implicit type='()')) (brace_stmt)))) (tuple_expr type='()' location=sample.swift:3:2 range=[sample.swift:3:2 - line:3:3]))) )) (var_decl "myVariable" type='Int' interface type='Int' access=internal let storage_kind=stored))
(closure_expr type='() -> Int' location=sample.swift:1:23 range=[sample.swift:1:23 - line:3:1] discriminator=0 let
myVariable: Int = { while(true) {} }()
let _: Int = { preconditionFailure() }() let _: Void
= { fatalError() }() let _: Never = { while(true) {} }()
class SomeClass { lazy var v: Int = { preconditionFailure("Variable
'\(#function)' used before being initialized") }()
class SomeClass { var v: Int! lazy var v: Int
= { preconditionFailure("Variable '\(#function)' used before being initialized") }()
Closure
let _: (Int) -> Int = { $0 + 1
}
func f(x: Int...) -> String { … } let _:
(Int...) -> String = f
struct SomeType { func f(x: Int) -> String { …
} } let _: (Int) -> String = SomeType().f let _: (SomeType) -> (Int) -> String = SomeType.f
enum Rank: Int { case ace = 1 case two,
three, four, five, six, seven, eight, nine, ten case jack, queen, king } let _: (Int) -> Rank? = Rank.init init?(rawValue: Int)
let _: (Int, Int) -> (Int) = (+) let _:
(inout Int, Int) -> () = (+=)
enum Page { case settings case externalSite(URL) let _: (URL)
-> Page = Page.externalSite }
static var settings: Page { return Page.settings } static func
externalSite(_ url: URL) -> Page { return Page.externalSite(url) } let _: (URL) -> Page = Page.externalSite enum Page { case settings case externalSite(URL) }
[1.1, 3.2 ,3.5].map(round) [1.1, 3.2 ,3.5].sorted(by: <) ["www.ezura.me", ""].flatMap(URL.init)
inout
let _: (Int...) -> String = f let _: (Int)
-> String = SomeType().f let _: (SomeType) -> (Int) -> String = SomeType.f let _: (Int, Int) -> (Int) = (+) let _: (inout Int, Int) -> () = (+=) let _: (URL) -> Page = Page.externalSite inout
static func +=(lhs: inout Self, rhs: Self) var x =
1 // x: 1 x += 1 // x: 2 inout
! error: Escaping closures can only capture inout parameters explicitly
by value func f(_ arg: inout String) -> () -> () { return { print(arg) } }
func f(_ arg: inout String) { arg = "" arg
= "" } var testString = "" { didSet { print("changed: \(testString)") } } f(&testString) changed:
change to “" change to “" func f(_ arg: inout
String) var testString "" "" "" "" ""
current testString: changed: func f(_ arg: inout String) { arg
= "" sleep(2) arg = "" } var testString = "" { didSet { print("changed: \(testString)") } } DispatchQueue.global().asyncAfter(deadline: .now() + .seconds(1)) { print("current testString: \(testString)") } f(&testString)
func f(_ arg: inout String) { arg = "" sleep(2)
arg = "" } var testString = "" /* { didSet { print("changed: \(testString)") } } */ DispatchQueue.global().asyncAfter(deadline: .now() + .seconds(1)) { print("current testString: \(testString)") } f(&testString) current testString:
change to “" change to “" func f(_ arg: inout
String) var testString "" "" ""
func f(_ arg: inout String) inout change to “" change
to “" var testString "" "" "" "" "" ""
inout return defer
defer execute after all other code in the scope
x++, ++x or neither? /* ??? */ func ++(x: inout
Int) -> Int { defer { x += 1 } return x }
postfix func ++(x: inout Int) -> Int { defer {
x += 1 } return x } x++
func f() { let v: String defer { print(v) }
v = "init value" }
func f() { let v: String defer { print(v) }
v = "init value" }
class SomeType { var v: String { didSet { …
} } init() { defer { v = " in defer" } v = "" v = "" } }
deinit { defer { v = "goodbye in defer" }
changeV() _ = { v = "goodbye in block" }() }
Swift Tour = (obj?.myVariable = 50) .map { /* …
*/ } { while(true) {} }() let _: (inout Int, Int) -> () = (+=) let _: (URL) -> Enum = Enum.case Closure
inout func f(_ arg: inout String) -> () -> ()
{ return { print(arg) } } copy-in copy-out / Optimized inout defer return
defer init() { defer { v = " in defer"
} v = " initialize" } deinit { v = "goodbye " defer { v = "goodbye in defer" } }
Swift Tour myVariable = 50 obj?.myVariable = 50 return Closure
defer didSet inout enum Never init, deinit
Thank you for listening!!