When I use internal access control level explicitly in Swift 4

1k Views Asked by At

I have a question about internal access control level

Internal is default access control level in Swift

so I think all of internal access control should be removed

Is there a specific case of using internal access control explicitly in Swift?

When or How I use internal access control in Swift?

3

There are 3 best solutions below

0
J. Doe On BEST ANSWER

I found a case internal needs to be added explicitly:

public internal(set) var myInt = 0

Omitting the internal keyword results in a compile error.

This is particular useful in a swift package/pod. The property is exposed publicly, but only inside the package/pod, the value can be changed.

3
ekscrypto On

As per documentation:

Default Access Levels All entities in your code (with a few specific exceptions, as described later in this chapter) have a default access level of internal if you don’t specify an explicit access level yourself. As a result, in many cases you don’t need to specify an explicit access level in your code.

Source: https://docs.swift.org/swift-book/LanguageGuide/AccessControl.html

As you mentioned, using the "internal" keyword has no effect other than making it clear that a function should never be made public in the future without careful consideration. At this point using the "internal" keyword is more about documenting and commenting your code.

1
gunas On

When use default access level internal, no need to add internal explicitly. This means that SomeInternalClass and someInternalConstant can be written without an explicit access-level modifier, and will still have an access level of internal. However the keyword "internal" is used just for developers understanding.

class SomeInternalClass {}              // implicitly internal
let someInternalConstant = 0            // implicitly internal