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
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With