Skip to content

Instantly share code, notes, and snippets.

@DeFrenZ
Created December 11, 2015 17:30
Show Gist options
  • Save DeFrenZ/06d393c69f1659df65b9 to your computer and use it in GitHub Desktop.
Save DeFrenZ/06d393c69f1659df65b9 to your computer and use it in GitHub Desktop.
protocol Foo: Hashable {
var id: Int { get }
}
extension Foo {
var hashValue: Int { return id }
}
extension Int: Foo {
var id: Int { return self }
}
class Manager {
func update <T: Foo> (t: T) {
print(t)
}
func callFoo <T> (v: T) {
print("call on", v, T.self)
switch v {
case is Foo: update(v)
case is SequenceType where Generator.Element: Foo: v.forEach { self.update($0) }
default: break
}
}
}
let m = Manager()
m.callFoo("a")
m.callFoo(1)
m.callFoo(["a"])
m.callFoo([1, 2])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment