Last active
August 29, 2015 14:02
-
-
Save wearhere/750b415c221bd7a11e9f to your computer and use it in GitHub Desktop.
Swift arrays are bullshit.
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
import Cocoa | |
let anArray = [1, 2, 3] | |
// Given `func foo(arr: Int[])`, | |
// how can it ensure the immutability of _arr_? | |
// 1. Won't work because `unshare` can only be called on vars. | |
//func foo(arr: Int[]) { | |
// arr.unshare() | |
//} | |
// 2. Won't work because `arr` is a constant. | |
//func foo(arr: Int[]) { | |
// arr = arr.copy() | |
//} | |
// 3. This isn't a solution because we don't want _arr_ to be mutable even internally. | |
//func foo(var arr: Int[]) { | |
// arr.unshare() | |
//} | |
// 4. Solution: Ignore the argument. | |
func foo (arr: Int[]) { | |
let constArr = arr.copy() | |
} | |
// The caller should also pass a copy of _anArray_ | |
// if it's going to use it after the call to _foo_, | |
// since it can't tell what _foo_ will or won't do. | |
foo(anArray.copy()) | |
// Two copies at every interface? So much for efficiency. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment