//TODO: professional stuff of software engineer 1001010
Swift: prefix/postfix ‘=’ is reserved

Neat – Apple released x-code 6 beta for free! Which means I finally got a chance to try swift in the “playground”.

As I go through the book and teach myself all about this shiny new tech bauble I may comment on it here.

OH LOOK, a scuff mark already

Playground looks interesting, but where’s the “run” button?  The book tells me to try “println("Hello World")” – but I can’t figure out how to launch it.  Oh well, I don’t care that much.  It looks like it’s real value is a teaching environment to play with code structures & simple flow – sort of like a code calculator.

First “code” section: “Simple Values”

var myVariable = 42

myVariable = 50
let myConstant = 42

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

That’s great – but I’m a terrible typist.  I ended up typing “var myVariable= 42”  And it errored.  WTF?

Swift irregular spaces around equal sign

As you can see, “x = y” and “x=y” are valid, but “x= y” and “x =y” are not.

W. T. F. ?

This.. is a new problem for me. In my experiences with C languages or Python, I’m not accustomed to casual compiler problems with spaces around an equal sign.  Looking at the error, there are a few things going through my head.

  1. The lexer is borked – it’s a new language after all, so not too unexpected
  2. Whitespace has meaning?  Yes, there are times where whitespace removes confusion in code.  Say in C++ where you have a template containing template params: std::map<int, std::vector<int>> needs to be std::map<int, std::vector> >” – but that’s a lexing issue.
  3. Someone is a fan of The Whitespace Programming Language
    It’s still early in the book – I hope they’re not a fan of brainfuck as well 🙁
  4. OK.. Let’s check the book

Damn it.  It’s in the book.  Here’s the relevant section..

Operators are made up of one or more of the following characters: /, =, -, +, !, *, %, <, >, &, |, ^, ~, and .. That said, the tokens =, ->, //, /*, */, ., and the unary prefix operator & are reserved. These tokens can’t be overloaded, nor can they be used to define custom operators.

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[…]

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

Matching space count and it’s a binary operator?  FOR EQUAL?

Space on one side only and it’s a PREFIX/POSTFIX UNARY OPERATOR?  FOR EQUAL?

 

Two pages in & two issues.  Maybe this is a journey into Brainfuck

 

-Jason De Arte

About

Categories: Swift Tags: , ,