Avatar
Builder にしておいたほうが、使用側の利便性はよさそうと思いました enum Valid {} enum Invalid {} struct Configurable { let configuration: Configuration } struct Configuration { fileprivate init(a: Int, b: String) { self.a = a self.b = b } let a: Int let b: String } struct ConfigurationBuilder<FlagA, FlagB> { private var a: Int = 0 func setFlagA(a: Int) -> ConfigurationBuilder<Valid, FlagB> { return ConfigurationBuilder<Valid, FlagB>(a: a, b: b) } private var b: String = "" func setFlagB(b: String) -> ConfigurationBuilder<FlagA, Valid> { return ConfigurationBuilder<FlagA, Valid>(a: a, b: b) } private init(a: Int, b: String) { self.a = a self.b = b } } extension ConfigurationBuilder where FlagA == Invalid, FlagB == Invalid { init() {} } extension ConfigurationBuilder where FlagA == Valid, FlagB == Valid { func build() -> Configuration { return Configuration(a: a, b: b) } } let configuration = ConfigurationBuilder() .setFlagA(a: 1) .setFlagB(b: "b") .build() _ = Configurable(configuration: configuration) (edited)
💯 1