Skip to content

Instantly share code, notes, and snippets.

@SeRG1k17
Created June 19, 2023 19:07
Show Gist options
  • Save SeRG1k17/b43190386c8af501a0de398442563d50 to your computer and use it in GitHub Desktop.
Save SeRG1k17/b43190386c8af501a0de398442563d50 to your computer and use it in GitHub Desktop.
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