Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
// here we have the type we are trying to extend the double
// notice how we can extend anything, even a built in type like Double
extension Double {
var km: Double { return self * 1_000.0 }
var m: Double { return self }
var cm: Double { return self / 100.0 }
var mm: Double { return self / 1_000.0 }
var ft: Double { return self / 3.28084 }
}
let oneInch = 25.4.mm
print("One inch is \(oneInch) meters")
// Prints "One inch is 0.0254 meters"
let threeFeet = 3.ft
print("Three feet is \(threeFeet) meters")
// Prints "Three feet is 0.914399970739201 meters"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment