I have a protocol like this:
protocol Robot {
func beep()
func boop()
func explode()
}
And a struct like this:
struct Factory {
let robot: Robot
func testRobot() {
robot.beep()
robot.boop()
}
func killRobot() {
robot.explode()
}
}
They both work fine (at least theoretically), and should work for anything with the protocol of Robot.
However, if I have a struct like this:
struct UselessRobot: Robot {
init() {
explode()
}
func explode() {
print("BANG")
}
}
Then XCode won't compile it, since I didn't define beep or boop. But, the functions would be empty if I defined them anyway:
extension Robot {
func beep() {}
func boop() {}
func explode() {}
}
Is there a way to get around this? It seems really weird to be writing lines of code that do nothing.
The Factory accepts any Robot, so it's easier to call beep and boop without worrying about if they work or not, so I'd rather avoid doing some complicated check if possible.
Thanks for any help!
EDIT: This question is the same as this one here, sorry everyone