Created
June 6, 2014 03:38
-
-
Save ketzusaka/12743fa9d6569dbb447d to your computer and use it in GitHub Desktop.
Swift Operator Overloading Adding << On Array
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
/* | |
In Swift, you can overload operators. A common operator in Ruby for working with arrays is <<, | |
which is syntatic sugar for array.push(). Since Array's in Swift don't take bitwise operations, this is | |
an example demonstrating adding that operator to an array that contains any type. | |
As an extra tidbit, I've added >> to prepend an array with an object | |
*/ | |
@infix func << <T>(inout array: Array<T>, item: T) { | |
array.append(item) | |
} | |
@infix func >> <T>(inout array: Array<T>, item: T) { | |
array.insert(item, atIndex: 0) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment