Skip to content

Instantly share code, notes, and snippets.

@sforteln
Last active August 29, 2015 14:20
Show Gist options
  • Save sforteln/158b72541d7240115fb3 to your computer and use it in GitHub Desktop.
Save sforteln/158b72541d7240115fb3 to your computer and use it in GitHub Desktop.
Swift generics and multiple protocols setup
//Given
protocol ProtocolA {
static var foo : String { get }
}
protocol ProtocolB {
static var bar : String { get }
}
//I want a function that will only accept types that impliment both protocols
class TypeA : ProtocolA, ProtocolB {
static var foo : String { get{ return "foo"} }
static var bar : String { get{ return "bar" } }
}
//Use where clause
func doStuff<T where T : ProtocolA, T : ProtocolB>(arg: T) {
// do stuff
}
// or Protocol Composition Type
func doStuff<T : protocol<ProtocolA, ProtocolB>>(arg: T) {
// do stuff
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment