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 Swift Basics (retired) Collections What is an Array?

John Wu
PLUS
John Wu
Courses Plus Student 3,288 Points

+= operator for Array is not a syntax

Amit said the todo += ["Pickup Laundry"] is a syntax, on which I don't agree with him.

When we say the word syntax, it should be something that came with the nature of the language. Let's say defining variables using var is a syntax.

I think += is not a syntax, but a result of Operator Overloading in Swift, which internally use the .append method on Array instances.

2 Answers

Ryan Field
PLUS
Ryan Field
Courses Plus Student 21,242 Points

In programming, when we talk about any aspect of how a specific language works, we refer to it as 'syntax'. You could say that "+=" is the correct syntax for adding onto a string in JavaScript, but in PHP, ".=" is the proper syntax.

"In computer science, the syntax of a computer language is the set of rules that defines the combinations of symbols that are considered to be a correctly structured document or fragment in that language."

Thats one definition, there are others all similar. I believe that "+=" or the "Augmented assignment" operator is correctly referred to as part of the language syntax.

Furthermore, operator overloading is a separate concept in and of itself to augmentation or the ".apend" method you referred to. Conceptually, in general when you overload a function, it means that you may use the same function name with different paramaters as long as they are defined in order to get different functionality.

For example if I had a function that had an overload defined I could call it like this:

callThisFunction(fiveTimes)

and:

callThisFunction(fiveTimes, atThisTime)

If you read the page you linked to it actually explained it very well..

"Typically, you’ll use overloading to extend an operation to a new object while maintaining the original semantics, rather than defining different (and confusing) behavior."