Skip to content

Instantly share code, notes, and snippets.

@beccadax
Forked from anonymous/Stupid overloading tricks
Last active August 29, 2015 14:05
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 beccadax/e2dc0672b0617b4a1e64 to your computer and use it in GitHub Desktop.
Save beccadax/e2dc0672b0617b4a1e64 to your computer and use it in GitHub Desktop.
extension String {
func split(separator:String) -> [String] {
return self.componentsSeparatedByString(separator)
}
}
extension Array {
func unpack() -> (T)? {
return count == 1 ? (self[0]) : nil
}
func unpack() -> (T, T)? {
return count == 2 ? (self[0], self[1]) : nil
}
func unpack() -> (T, T, T)? {
return count == 3 ? (self[0], self[1], self[2]) : nil
}
func unpack() -> (T, T, T, T)? {
return count == 4 ? (self[0], self[1], self[2], self[3]) : nil
}
func unpack() -> (T, T, T, T, T)? {
return count == 5 ? (self[0], self[1], self[2], self[3], self[4]) : nil
}
func unpack() -> (T, T, T, T, T, T)? {
return count == 6 ? (self[0], self[1], self[2], self[3], self[4], self[5]) : nil
}
func unpack() -> (T, T, T, T, T, T, T)? {
return count == 7 ? (self[0], self[1], self[2], self[3], self[4], self[5], self[6]) : nil
}
func unpack() -> (T, T, T, T, T, T, T, T)? {
return count == 8 ? (self[0], self[1], self[2], self[3], self[4], self[5], self[6], self[7]) : nil
}
func unpack() -> (T, T, T, T, T, T, T, T, T)? {
return count == 9 ? (self[0], self[1], self[2], self[3], self[4], self[5], self[6], self[7], self[8]) : nil
}
func unpack() -> (T, T, T, T, T, T, T, T, T, T)? {
return count == 10 ? (self[0], self[1], self[2], self[3], self[4], self[5], self[6], self[7], self[8], self[9]) : nil
}
}
if let (hello, world) = "Hello, world".split(", ").unpack() {
println(hello)
println(world)
}
if let (hello, world) = "Hello, world, omg".split(", ").unpack() {
println(hello)
println(world)
}
if let (first, last, birthday) = "Bob, Quone, January 1st 1987".split(", ").unpack() {
println(first)
println(last)
println(birthday)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment