Skip to content

Instantly share code, notes, and snippets.

@chriseidhof
Last active August 29, 2015 14:02
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chriseidhof/f51b45f84bb15311fd20 to your computer and use it in GitHub Desktop.
Save chriseidhof/f51b45f84bb15311fd20 to your computer and use it in GitHub Desktop.
Swift Constraints on extension?
enum Sum<L,R> {
case Left(@auto_closure () -> L)
case Right(@auto_closure () -> R)
}
protocol GShow {
func gshow() -> String
}
extension String : GShow {
func gshow() -> String {
return self
}
}
func show<L : GShow, R : GShow>(x : Sum<L,R>) -> String {
switch x {
case .Left(let l):
return l().gshow()
case .Right(let r):
return r().gshow()
}
}
/* How to do: */
extension Sum : GShow {
func gshow() -> String {
let s = show(self) // This line gives an error
return s
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment