Created
November 1, 2017 06:09
-
-
Save zakky-dev/171d80475210190fc8eef670c1627a19 to your computer and use it in GitHub Desktop.
型パラメータによって有効になったり無効になったりするextension
This file contains hidden or 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
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