4

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?

5
  • Maybe while subclassing and you override variables? Commented Aug 9, 2018 at 6:11
  • internal marks the property available in the module only, it is really important when developing a library or framework – obviously it must not be removed. Commented Aug 9, 2018 at 7:36
  • @holex He means adding the keyword explicitly. Commented Aug 17, 2018 at 20:45
  • @J.Doe, then the best way is to make an proposal on Swift Evolution Forums about removing the keyword entirely and see what community says, if that keyword seems redundant. Commented Aug 20, 2018 at 7:28
  • @holex Since the keyword doesn't have to be included every time, it isn't redundant, the same applies for the keyword noescaping. Commented Aug 20, 2018 at 8:28

3 Answers 3

6

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.

Sign up to request clarification or add additional context in comments.

Comments

1

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.

3 Comments

And yet for some reason, if you look at all of apples packages, internal is explicitly specified everywhere.
@Era yes, Apple like to make things explicit and well documented.
Oh, is that why almost every new API nowadays has 0 documentation or not even a doc comment :D
0

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 

1 Comment

I came up with something new whereas internal really needs to be added explicitly this time :P

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.