UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

Day 11: Protocols and Extensions Summary Quiz --- Can a struct inherit from a protocol?

Forums > 100 Days of Swift

The following code is invalid:

protocol CanFly {
    var maximumFlightSpeed: Int { get set }
}
protocol CanDrive {
    var maximumDrivingSpeed: Int { get set }
}
struct FlyingCar: CanFly, CanDrive { }

I'm trying to wrap my head around protocol inheritance. In this example, I see that a Struct is using protocol inheritance. However, it needs to add in the 'vars' from the previous protocols CanFly and CanDrive so the code can work. I have tested this in Playground.

Why?

And additionally, I seem to recall some form of inheritance where you don't need to add in the vars from the parent protocols. Or am I just confusing this with Classes?

3      

As an additional post, my confusion is increased when viewing this from my notes:

//Payable requires conforming types to implement a calculateWages() method
protocol Payable {
    func calculateWages() -> Int
}
//NeedsTraining requires conforming types to implement a study() method
protocol NeedsTraining {
    func study()
}
//HasVacation requires conforming types to implement a takeVacation() method:
protocol HasVacation {
    func takeVacation(days: Int)
}

//Protocol Inheritance
//Now we can make new types conform to that single protocol rather than each of the three individual ones
protocol Employee: Payable, NeedsTraining, HasVacation { } 

This is from the lesson on Protocol Inheritance. It seems 'protocol Employee' is inheriting from Payable, NeedsTraining, and HasVacation, but clearly is not elaborating upon any of the func found in any of those parent protocols. It's simply empty { }.

Why is this valid, but in my previous post, the struct needs to elaborate upon the previous vars?

3      

In this example, I see that a Struct is using protocol inheritance.

This is not protocol inheritance. This is the FlyingCar struct adopting or conforming to the CanFly and CanDrive protocols.

To quote from the language guide:

A protocol defines a blueprint of methods, properties, and other requirements that suit a particular task or piece of functionality. The protocol can then be adopted by a class, structure, or enumeration to provide an actual implementation of those requirements. Any type that satisfies the requirements of a protocol is said to conform to that protocol.

So classes, structs and enums do not inherit from a protocol, they adopt it and implement the blueprint laid out by the protocol.

However, it needs to add in the 'vars' from the previous protocols CanFly and CanDrive so the code can work. I have tested this in Playground.

Why?

Because any struct, class or enumeration that conforms to a protocol has to implement the required properties and methods of that protocol. Since you state that FlyingCar conforms to CanFly and CanDrive, it has to implement the required properties of those protocols.

This is from the lesson on Protocol Inheritance. It seems 'protocol Employee' is inheriting from Payable, NeedsTraining, and HasVacation, but clearly is not elaborating upon any of the func found in any of those parent protocols. It's simply empty { }.

Protocols, on the other hand, can inherit from other protocols. Using your example, the Employee protocol inherits the methods defined in the Payable, NeedsTraining, and HasVacation protocols but any struct, class or enum that adopts the Employee protocol will have to implement those methods.

Protocol inheritance is basically just a shorthand way of grouping protocols that are frequently used together. Or it can provide a way to compose protocols from smaller pieces.

You could do this:

struct Drone: Payable, NeedsTraining, HasVacation {
    //and implement the required properties and methods
}

Or you can use protocol inheritance to create the Employee protocol and do this:

struct Drone: Employee
    //and implement the required properties and methods
}

Same end result.

To throw a further wrench into things, protocols can have default implementations. But I'll leave that for a future discovery.

I highly recommend reading the language guide page on protocols.

3      

BUILD THE ULTIMATE PORTFOLIO APP Most Swift tutorials help you solve one specific problem, but in my Ultimate Portfolio App series I show you how to get all the best practices into a single app: architecture, testing, performance, accessibility, localization, project organization, and so much more, all while building a SwiftUI app that works on iOS, macOS and watchOS.

Get it on Hacking with Swift+

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.