Something like Java packages / namespaces?

I've been working on a larger app this week. The app-global namespace for types and filenames is getting annoying. What are people doing here? Are there any proposals to add a language feature?

I see some use/abuse of enum for this. I may try that, though I guess it doesn't work for protocols, or file names.

  enum MyNamespace { ... }

I also saw something about breaking up an app into Swift packages. I've used Swift packages for code that's reused among multiple apps, but haven't tried it within a single app like that.

Right now I'm just prefixing names like I'm writing C-code.

2 Likes

Yup, this is a pain point that various members of the Swift team are aware of. The only workarounds are to try to minimise the scopes of your imports as much as possible, but there are altogether too many hidden pitfalls that make this hard (e.g. do not have a type in a module whose name is the same as that module).

There will need to be work done in this area for sure.

Currently there is one moderately ugly workaround (proposed by @Karl) — defining an underscored protocol on top level and then creating a namespaced typealias:

public protocol _HiddenFeatureProtocol {
    // requirements
}

public enum Namespace {
    typealias FeatureProtocol = _HiddenFeatureProtocol
}

public struct FeatureImpl: Namespace.FeatureProtocol {
    // implementation
}
2 Likes

Can't wait for generalization of generic system to be picked up again, hopefully after the whole async topic. That could finally resolve this problem. If we had generic protocols (:speak_no_evil:) nesting would be an issue.

Terms of Service

Privacy Policy

Cookie Policy