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

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

Vansh Agarwal
PLUS
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.