Created
March 13, 2017 15:35
-
-
Save hborders/d2db2183fdb3d84ad1047f9faa698ecc to your computer and use it in GitHub Desktop.
The swift compiler saves us from mismatching our enum types
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
enum Example { | |
case Foo(f: String, g: String) | |
case Bar(b: Int, c: Int) | |
} | |
func printExample(_ example: Example) { | |
switch example { | |
case .Foo(let f, let g): | |
print("Foo: \(f), \(g)") | |
case .Bar(let b, let c): | |
print("Bar: \(b), \(c)"); | |
} | |
} | |
func badPrintExample(_ example: Example) { | |
switch example { | |
case .Foo(let b: Int, | |
// ^ | |
// Swift:: Error: use of unresolved identifier 'b' | |
let c: Int): | |
// ^ | |
// Swift:: Error: use of unresolved identifier 'c' | |
print("Foo: \(b), \(c)") | |
case .Bar(let f: String, | |
// ^ | |
// Swift:: Error: use of unresolved identifier 'f' | |
let g: String): | |
// ^ | |
// Swift:: Error: use of unresolved identifier 'g' | |
print("Bar: \(f), \(g)"); | |
} | |
} | |
let fooExample = Example.Foo(f: "foo", g: "goo") | |
let barExample = Example.Bar(b: 2, c: 3) | |
let badFooExample = Example.Foo(b: 2, c: 3) | |
// ^ | |
// Swift:: Error: incorrect argument labels in call (have 'b:c:', expected 'f:g:') | |
let badBarExample = Example.Bar(f: "foo", g: "goo") | |
// ^ | |
// Swift:: Error: incorrect argument labels in call (have 'f:g:', expected 'b:c:') | |
badPrintExample(fooExample) | |
printExample(badFooExample) | |
badPrintExample(barExample) | |
printExample(badBarExample) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment