Encapsulating data in Swift

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?

+6
source share
3 answers

It is simply missing at the moment. Greg Parker explicitly stated ( in this developer forum ) that visibility modifiers are approaching.

Given that there are no headers, the standard Objective-C tricks will not work, and I cannot come up with another trick to limit visibility, which does not involve a lot of bending backwards. Since the language feature was promised, I'm not sure if it is worth the big investment.

On the bright side, since this function is in motion, now is the time to record the radar and influence how it works.

+6
source

Updated answer for future reference.

From Apple documentation :

Access levels

Swift provides three different access levels for an entity within your code. These access levels relate to the source file in which the entity is defined, and also relative to the module to which the source file belongs.

Open access allows objects to be used in any source file from their defining module, as well as to the source file from another module that imports the defining module. Usually you use open access when specifying a common interface to the structure.

Internal access allows an entity to be used in any source file from its defining module, but not in any source file outside this module. You usually use internal access when defining applications or the internal structure of the framework.

Private access restricts the use of an object in its own defining source file. using private access to hide the details of the implementation of a particular part of the functionality. Open access is the highest (least restrictive) access level and private access is the lowest (or most restrictive) access level.

+4
source

In fact, I was delighted that Swift finally adopted a static typing that is consistent with the theory for code with optimal OO properties, but dropping the headers breaks the very mindset of Object Orienting programming, namely encapsulation. The output would be like Eiffel automatically retrieving the headers, but without specifying which public interfaces and which private, nonetheless. I really criticized this step by Apple.

0
source

Source: https://habr.com/ru/post/970525/


All Articles