1
normanzb
311d

in apple's blog they explained why they don't want a `protected` in swift:

https://developer.apple.com/swift/...

> It doesn’t actually offer any real protection, since a subclass can always expose “protected” API through a new public method or property."

Isn't the same thinking applies to `internal` keyword as well? Yet they allow `internal` to be there as default modifier for `class` in a package. Also I don't think `protected` is for the sole purpose of "protection", but for the cleanliness of externally visible interface, some methods are just useless to be exposed and will confuse the consumer if they don't understand internally how the class works. So it doesn't have to be 100% securely `protected` (arguably the term `protected` is a poor choice tho).

but hey, it is apple, being opinionated doesn't surprise me.

Comments
  • 1
    whatever happened to "public"/"private" for this purpose?
  • 0
    This is going to be apples and oranges, but coming from PHP, everything is either public or protected.

    I rarely see private modifiers.
  • 2
    My problem with protected is that it encourages using inheritance where the derived class is not in fact a valid replacement for the base, and adding responsibilities to the derived class according to their data dependencies rather than their capabilities.

    Any protected method that user code can safely use can be public. All incentives for hiding a method from external code other than aesthetics also apply to external subclasses.
  • 0
    By their logic, visibility modifiers make no sense in general as you can use the Mirror API to get around them.
  • 0
    use composition rather than inheritance.

    Suddenly "protected" will be irrelevant.
  • 0
    @Parzi doesn't protected mean "same as private, but visible to subclasses" (in e.g. C++)? It's not the same as public or private as I understand it
Add Comment