News for hackers. Stuff that matters.
Re:Swift 2.1 by seivan on Thursday September 24, 2015 @06:11PM (#10275112)
Actually, some of those features are still buggy when operating to Obj-C land :)

      //Warning if you don't use let :-) 
      let array = NSMutableArray(capacity: 50)
      array.addObject(notification)

I am aware that the let essentially means I can't reassign the variable, and does not have anything to do with the mutability of the class. Since it's a class and not a struct.

However, what I am saying is that Apple needs to somehow add the keywords to the container classes imported from Cocoa, this is manually I guess.

I wonder if adding a mutating keyword on the declaration would help

    public mutable func addObject(anObject: AnyObject) mutable
That way that should throw a warning on a let usage. But that keyword only exists on Swift. They would have to go through all of them like they did with nullable keyword.

If you're wondering why I am using CFArray -> performance. Structs are pretty slow since you'd have to copy it from the dictionary to mutate and then insert into the dictionary again.

Though, those numbers were with Swift 1. Will try with Swift 2 again, but given the nature of immutability, I suspect it will still be slower.

Now that I think about it. I might make my own generic structs that uses NSArray/NSDictionary internally. Though I wonder if you can pass structs by reference instead of copy, that would be more useful.

Re:Swift 2.1 by xenadu02 on Thursday September 24, 2015 @11:30PM (#10276241)
Remember that Swift collection types have the semantics of copy-on-write. Under the hood they actually only copy if the collection is non-uniquely referenced.

An Array<T> is a struct but uses a storage reference type under the covers. In theory the standard library is actually free to make common slices of arrays share storage so pure additions don't even require copying but I have no idea if it is actually implemented that way (neither String nor Array guarantee contiguous storage)

Re:Swift 2.1 by hellofunk on Friday September 25, 2015 @06:28AM (#10277265)
It definitely changes how a developer works in a language if they know they can depend on this kind of implementation. In Clojure, knowing up front that all data structures have shared persistence allows you to do things that you'd never consider in a language where this wasn't guaranteed.

I think Swift could greatly enhance its power in the functional paradigm if indeed it did what you describe, but also making this clearly known to everyone who uses it.

Re:Swift 2.1 by mattdw on Thursday September 24, 2015 @06:28PM (#10275210)
Swift seems to distinguish 'mutating' from 'changing'. The name 'array' in your example remains pointing to the same object, it's just that that object is being internally mutated. Not sure that there'd be a foolproof way to catch all possible mutations, so that may be the best they can do.
Re:Swift 2.1 by supster on Thursday September 24, 2015 @06:49PM (#10275321)
For objects passed by reference, a Swift "let" signifies immutability of reference (not value); thus you can append to the NSMutableArray but not reassign to a different NSMutableArray. On the other hand a native Swift Array assigned via "let" would be immutable from both a value and assignment perspective.
Re:Swift 2.1 by ecopoesis on Thursday September 24, 2015 @07:52PM (#10275609)
That's not a bug. Your array reference isn't changing, just the data in the object. Scala (which uses var and val for mutable and immutable) would do exactly the same thing.

Heck, even Java (using final to mark immutable) would work like this. This is basically how every OO language works.

Re:Swift 2.1 by myko on Thursday September 24, 2015 @08:20PM (#10275733)
I think the poster understands that, it's just in Swift if you assign a Swift Array (not an NSArray) using let it is immutable both in reference and in whether or not items can be added or removed from it.

That said, it's a quirk of the interop and I don't think Apple should spend time fixing it - rather developers should avoid NSArray within Swift.

Re:Swift 2.1 by seivan on Friday September 25, 2015 @02:22AM (#10276642)
Can't avoid it for everything which is why I'm considering wrapping.
Re:Swift 2.1 by solomone on Thursday September 24, 2015 @06:16PM (#10275145)
I wouldn't call that a bug. That's like saying you can't do

  let a = Foo()
  a.addItem(i)
Re:Swift 2.1 by seivan on Thursday September 24, 2015 @06:41PM (#10275272)
It's a bug in a sense that NSArray, NSSet, NSDictionary (The mutable ones) should have the keywords setup when they are ported with Swift headers.

Naturally, that would be manual work though. Like the way they added nonnull, nilable or whatever its called to Obj-C

Re:Swift 2.1 by mikeash on Thursday September 24, 2015 @09:10PM (#10275914)
Why would they have keywords added? Adding `mutating` to a func in a class is invalid, because the very concept makes no sense. The `mutating` keyword can only apply to value types, because it means "this func mutates the value in-place, so it needs mutable storage to invoke it."

The "bug," if there can be said to be one at all (I don't see it), is that Swift collection types are value types, while Objective-C collection types are reference types. But given this difference, the lack of `mutating` on the mutating methods makes complete sense, and adding it would be bizarre, since it would be an error if you did it in your own code.