Last active
August 29, 2015 14:02
-
-
Save chriseidhof/f51b45f84bb15311fd20 to your computer and use it in GitHub Desktop.
Swift Constraints on extension?
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
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