Created
June 19, 2023 19:07
-
-
Save SeRG1k17/b43190386c8af501a0de398442563d50 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
protocol Vehicle { | |
var name: String { get } | |
associatedtype FuelType | |
func fillGasTank(with fuel: FuelType) | |
} | |
struct Car: Vehicle { | |
let name = "car" | |
func fillGasTank(with fuel: Gasoline) { | |
print("Fill \(name) with \(fuel.name)") | |
} | |
} | |
struct Bus: Vehicle { | |
let name = "bus" | |
func fillGasTank(with fuel: Diesel) { | |
print("Fill \(name) with \(fuel.name)") | |
} | |
} | |
struct Gasoline { | |
let name = "gasoline" | |
} | |
struct Diesel { | |
let name = "diesel" | |
} | |
@resultBuilder | |
struct Builder { | |
static func buildEither<V>(first component: V) -> V where V: Vehicle { | |
component | |
} | |
static func buildEither<V>(second component: V) -> V where V: Vehicle { | |
component | |
} | |
static func buildBlock<V>(_ component: V) -> V where V: Vehicle { | |
component | |
} | |
} | |
@Builder func createSomeVehicle(isPublicTransport: Bool) -> some Vehicle { | |
if isPublicTransport { | |
Bus() | |
} else { | |
Car() | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment