Have you always been able to use leading-dot syntax with "namespacing"?

I've used the ability to extend types with static members that return Self, so you can use leading dot syntax, for like ever.

extension String {
  static let stringyDingyDoo = "doo"
}
let string: String = .stringyDingyDoo

But, inspired by all the organizational fun we have these days due to SE-0299, I only recently thought to try this:

extension String {
  enum Stringy {
    enum Dingy {
      static let doo = "doo"
    }
  }
}
let string: String = .Stringy.Dingy.doo

That's awesome. Could you always do that?

2 Likes

That capability was added in Swift 5.4 (which feels way more recent than I thought) via this proposal: swift-evolution/proposals/0287-implicit-member-chains.md at main · swiftlang/swift-evolution · GitHub

2 Likes

I misread your example. This is a nice discovery.

SE-0287 does not mention nested types, but it apparently works nowadays, thanks!

Thanks! But it's really not obvious! Aside from not mentioning what I showed, in the examples, it's also not even listed as a possibility.

I generally see people make enums to house this kind of stuff, but never nested in extensions. That dot is really helpful cognitively! And it's not bad for autocomplete, either.

2 Likes