Skip to content

Instantly share code, notes, and snippets.

@wearhere
Last active August 29, 2015 14:02
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 wearhere/750b415c221bd7a11e9f to your computer and use it in GitHub Desktop.
Save wearhere/750b415c221bd7a11e9f to your computer and use it in GitHub Desktop.
Swift arrays are bullshit.
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