Or: Fun with error messages!
Let‘s start with some crappy code I wrote in the Playground as I was going through the Swift language book.
let dict = [String:Float]() dict dict["Bob"] = 1.0 dict["Joe"] = "dude" dict
Guess which dictionary insert line generated this error?
'@value $T5' is not identical to '(String, Float)'
(insert Jeopardy suspenseful waiting music here)
If you’ve never written Swift I’m sure you’re probably at least partially right,
it was the “Joe” insertion
..and it was also the “Bob” insertion!
There are 3 problems here
- Mine: The dictionary is a
String:Float, so setting a
Stringvalue is wrong.
- Mine: The dictionary is “
let” not “
var“, so it’s read-only. Insertions are not allowed.
The quick fix is to make it “
var dict = [String:Float]()“
- Swift: It’s a crappy error message. If it’s read only – then say it’s read only.
Dear Swift Language team at Apple,
When a user assigns a value to a read-only variable, please tell them root problem – not the immediate error message. By being cryptic & indirect – you are reminding me of C++ template errors.
-Jason De Arte
Apple seems to be raising the bar on auto correcting code editor UI
From the code in the swift book
let emptyArray = String()
Soon after I typed it in, Playground prompted me to use the new syntax – AND FIX IT IN PLACE
A quick click transformed it to
let emptyArray = [String]()
Nice Apple, very nice.
Now if they would also do the Auto “Fix-it” for dictionary declarations 😉
-Jason De Arte
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?
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.
- The lexer is borked – it’s a new language after all, so not too unexpected
- 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.
- 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 🙁
- 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