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 trialWill Matthews
8,127 PointsWhy use an extension here?
In the example, we used an extension to add the Equatable conformance to Item. What rationale should we be considering when using an extension versus adding the conformance to the struct itself?
3 Answers
miikis
44,957 PointsHi Will,
It's a common convention in Swift to use extensions
to break apart the functionality of a class/structure into different sections, usually according to conformance to some protocol(s). It makes things more readable. Still, there's nothing preventing you from not doing things this way.
Jeff McDivitt
23,970 PointsAn extension allows a computed property
Will Matthews
8,127 PointsWe're using a type method in this case, though, not a computed property. As well, a computed property could surely be included in the definition of the object rather than only in an extension, no?
I'm just wondering, was there a specific reason that we added the Equatable-conforming method using an extension rather than just adding it to the object definition, or was it mainly just to get more practice working with extensions? ie. is there some rationale we should be using when choosing between extensions and editing our own objects?
Will Matthews
8,127 PointsWill Matthews
8,127 PointsGotcha. I think that's what I've been seeing in some of the examples that follow this one too. Makes sense. Thanks!