Danny
March 14, 2025, 1:21am
1
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
nnnnnnnn
(Nate Cook)
March 14, 2025, 4:51am
2
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!
Danny
March 14, 2025, 6:17am
4
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