Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial


Ilhyun Jo
Ilhyun Jo
6,288 Points

Can you have more than one type constraints for T?

While thinking about the assignment at the end I was wondering if something like func myFunc<T: protocol1, protocol2> was possible. So that T has to conform to both protocols.

I was thinking about making T conform to both comparable and equatable(or something).

1 Answer

Martin Wildfeuer
Martin Wildfeuer
Courses Plus Student 11,071 Points

The following should do:

func test<T where T: Equatable, T: Comparable>(test: T) {}

However, the Comparable protocol already inherits from Equatable, so it's not really a good example.

public protocol Comparable : Equatable {}

Hope that helps :)