Skip to content

Instantly share code, notes, and snippets.

@cfr
Created May 24, 2015 07:55
Show Gist options
  • Save cfr/c14dcc9c950872331f30 to your computer and use it in GitHub Desktop.
Save cfr/c14dcc9c950872331f30 to your computer and use it in GitHub Desktop.
protocol Convertible {
func to<T>() -> T;
// var <T>to: T { get } // multiple instances of generic var is not allowed
}
extension String {
func to() -> String {
return self
}
func to() -> Int {
return (self as NSString).integerValue
}
func to() -> Double {
return (self as NSString).doubleValue
}
}
let s = "5.1"
let d: Double = s.to() // 5.1
let i: Int = s.to() // 5
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment