I read the entire Swift book and watched all the WWDC videos (all that I wholeheartedly recommend). One thing I'm worried about is data encapsulation.
Consider the following (completely contrived) example:
class Stack<T> { var items : T[] = [] func push( newItem: T ) { items.insert( newItem, atIndex: 0 ) } func pop() -> T? { if items.count == 0 { return nil; } return items.removeAtIndex( 0 ); } }
This class implements the stack and implements it using an array. The problem is that items (like all properties in Swift) are publicly available, so nothing prevents anyone from directly accessing (or even mutating) from him separately from the public API. As a smoking old guy in C ++, it makes me very angry.
I see how people complain about the lack of access modifiers, and although I agree that they will directly solve the problem (and I hear rumors that they can be implemented in the near future (TM)), I wonder what strategies for data hiding will be their absence.
Am I missing something, or is it just an omission in this language?
source share