Skip to content

Instantly share code, notes, and snippets.

@hborders
Created March 13, 2017 15:35
Show Gist options
  • Save hborders/d2db2183fdb3d84ad1047f9faa698ecc to your computer and use it in GitHub Desktop.
Save hborders/d2db2183fdb3d84ad1047f9faa698ecc to your computer and use it in GitHub Desktop.
The swift compiler saves us from mismatching our enum types
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