Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

iOS Generics in Swift Generic Functions, Parameters and Constraints Protocol Based Type Constraints

Vansh Agarwal
Vansh Agarwal
Courses Plus Student 3,965 Points

== static method

When calling the == static method(which conforms to the Equatable protocol), why don't we have to call it like a normal method? Eg: Instead of saying something like Item.==(lhs: Item(quatity: 2, price: 3), rhs: Item(quatity: 3, price: 4) why can say Item(quatity: 2, price: 3) == Item(quatity: 3, price: 4) *both of these to return a boolean value

I really hope you have understood my question. If not you can just ask me for more clarity.

1 Answer

Correct me if I'm wrong, but operators in Swift are implemented in such a way that you can define your own. Operators can either be prefix, infix or postfix. If you don't specify anything, they default to infix. The == operator is an infix operator, because it appears between two operands, like value1 == value2. I guess the answer to your question is that you can't write Item.==(lhs:Item1, rhs:Item2) is because the == operator isn't a method that takes arguments, but rather an operator that needs operands.