Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When I use internal access control level explicitly in Swift 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?

like image 555
Cruz Avatar asked Mar 05 '23 15:03

Cruz


2 Answers

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.

like image 74
J. Doe Avatar answered Apr 29 '23 20:04

J. Doe


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.

like image 33
ekscrypto Avatar answered Apr 29 '23 21:04

ekscrypto