Last active
August 29, 2015 14:20
-
-
Save sforteln/158b72541d7240115fb3 to your computer and use it in GitHub Desktop.
Swift generics and multiple protocols setup
This file contains 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
//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