Skip to main content
Journey Uncommon Logo
JourneyUncommon
mediumSwiftphantom typesSingle-choice MCQ

Consider a builder that uses a phantom state to enforce that `build()` is only callable after `setName` was called. What does the compiler enforce here?

enum Unset {} enum Set {} struct Builder<NameState> { private var name: String? init() where NameState == Unset { self.name = nil } private init(name: String?) { self.name = name } func setName(_ n: String) -> Builder<Set> { Builder<Set>(name: n) } } extension Builder where NameState == Set { func build() -> String { name! } }