Skip to content

Instantly share code, notes, and snippets.

@cprovatas
Last active July 22, 2023 02:04
Show Gist options
  • Save cprovatas/d07226c3b8f4bd37dd6232d9ed013d6a to your computer and use it in GitHub Desktop.
Save cprovatas/d07226c3b8f4bd37dd6232d9ed013d6a to your computer and use it in GitHub Desktop.
Spread Syntax for Swift Arrays (ala es6)
extension Array {
static func ... (lhs: [Self.Element], rhs: [Self.Element]) -> [Self.Element] {
var copy = lhs
copy.append(contentsOf: rhs)
return copy
}
static func ... (lhs: Self.Element, rhs: [Self.Element]) -> [Self.Element] {
var copy = [lhs]
copy.append(contentsOf: rhs)
return copy
}
static func ... (lhs: [Self.Element], rhs: Self.Element) -> [Self.Element] {
var copy = lhs
copy.append(rhs)
return copy
}
}
let foo: [String] = ["1"]...["2", "3"]
print(foo) // ["1", "2", "3"]
let bar: [String] = "1"...["2", "3"]
print(bar) // ["1", "2", "3"]
let baz: [String] = ["1", "2"]..."2"
print(baz) // ["1", "2", "2"]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment