Skip to content

Instantly share code, notes, and snippets.

@RedGhoul
Created June 16, 2018 02:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save RedGhoul/7a61bf6ef91b920e8ddaf4d7872771e8 to your computer and use it in GitHub Desktop.
Save RedGhoul/7a61bf6ef91b920e8ddaf4d7872771e8 to your computer and use it in GitHub Desktop.
// 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