Skip to content

Instantly share code, notes, and snippets.

@ahti
Created June 4, 2014 13:47
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 ahti/bd96477ebd631fecb98d to your computer and use it in GitHub Desktop.
Save ahti/bd96477ebd631fecb98d to your computer and use it in GitHub Desktop.
Swift subscript overloading and vargs
class Foo {
subscript(idxs: Int...) -> Int {
get {
return idxs.reduce(1, combine: *)
}
}
subscript(idxs: Array<Int>) -> Int {
get {
return idxs.reduce(1, combine: *)
}
}
}
let a = Foo(), list = [1, 2, 5]
a[list] // => 10
a[1,2,3] // => 6
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment