Having a top-level private class, for example:
private final class Bar {}
then, I'm able to extend it in an extension (in the same file), for example:
extension Bar {
static func baz() {}
}
However, when I have a local private class:
enum Foo {
private final class Bar {}
}
and try to extend it (in the same file):
private extension Foo.Bar {
static func baz() {}
}
The compiler emits the error
private extension Foo.Bar
^ 'Bar' is inaccessible due to 'private' protection level.
A use case would be when using the Observation module (from Version 15.0 beta 5 (15A5209g)), where for example a "Model" class, has been put into some name space, say "MyFeature":
import Observation
enum MyFeature {
@Observable
private final class Model {
...
}
}
This wont compile, since the expansion of macro @Observable
adds an extension (in file scope) like:
extension MyFeature.Model: Observation.Observable {}
and the compiler complains. The extension should also be private, if I'm correct? But anyway, it seems, this is a language issue.
Well, one could of course change the accessibility of the class Model
to internal
or public
– but, IMHO, this would be against best practices, if the symbol "Model" would and should be only visible in this file.