Last active
July 22, 2023 02:04
-
-
Save cprovatas/d07226c3b8f4bd37dd6232d9ed013d6a to your computer and use it in GitHub Desktop.
Spread Syntax for Swift Arrays (ala es6)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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