Skip to content

Instantly share code, notes, and snippets.

@zakky-dev
Created November 1, 2017 06:09
Show Gist options
  • Save zakky-dev/171d80475210190fc8eef670c1627a19 to your computer and use it in GitHub Desktop.
Save zakky-dev/171d80475210190fc8eef670c1627a19 to your computer and use it in GitHub Desktop.
型パラメータによって有効になったり無効になったりするextension
protocol InitWithIntProtocol {
init(i: Int)
}
class WithInt: InitWithIntProtocol {
required init(i: Int) {
print(i)
}
}
class ExType<T> {
var value: T
init(v: T) {
self.value = v
}
}
extension ExType where T: CustomStringConvertible {
var description: String {
get {
return self.value.description
}
}
}
extension ExType where T: InitWithIntProtocol {
func newValue() -> T {
return T(i: 42)
}
}
let v = ExType(v: WithInt(i: 21))
print(v.newValue())
//print(v.description) // compile error
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment