Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is this response from the compiler valid?

Tags:

swift

The following code invokes an error. I could not find any information on this is in the reference. The lack of whitespace on the right hand side of the '=' operator is an error.

let names =["Anna", "Alex", "Brian", "Jack"]

Any other combination of this syntax compiles. Anyone know if this is truly invalid syntax per what we know of Swift right now?

EDIT: Error response is: Prefix/postfix '=' is reserved

ANSWER: This excerpt seems to answer my question. I just couldn't find it for the longest time:

The whitespace around an operator is used to determine whether an operator is used as a prefix operator, a postfix operator, or a binary operator. This behavior is summarized in the following rules:

If an operator has whitespace around both sides or around neither side, it is treated as a binary operator. As an example, the + operator in a+b and a + b is treated as a binary operator. If an operator has whitespace on the left side only, it is treated as a prefix unary operator. As an example, the ++ operator in a ++b is treated as a prefix unary operator. If an operator has whitespace on the right side only, it is treated as a postfix unary operator. As an example, the ++ operator in a++ b is treated as a postfix unary operator. If an operator has no whitespace on the left but is followed immediately by a dot (.), it is treated as a postfix unary operator. As an example, the ++ operator in a++.b is treated as a postfix unary operator (a++ . b rather than a ++ .b).

Excerpt From: Apple Inc. “The Swift Programming Language.” iBooks. https://itun.es/us/jEUH0.l

like image 606
dans3itz Avatar asked Jun 04 '14 03:06

dans3itz


2 Answers

Add a space after the =. (=[ looks too sad to be an operator.) It's probably seeing =value as a use of a (possible, but not implemented) prefix operator.

Swift isn't entirely whitespace-agnostic like C... in particular, it uses whitespace to distinguish prefix from postfix operators (because ++i++ in C is a grammar oddity). But it's not ridiculously strict about whitespace like Python either.

like image 197
rickster Avatar answered Oct 18 '22 18:10

rickster


Try adding a space between the = and [.

When the equals sign is directly in front of the bracket, the compiler assumes that you are trying to perfom a prefix operation on the array.

like image 24
Connor Avatar answered Oct 18 '22 19:10

Connor