Pages

Saturday, November 24, 2018

It's time new languages came with linters

From an introduction to Swift I find myself reading today:

But what if we forget the verification like below?

var stockCode:String? = findStockCode("Facebook")
let text = "Stock Code - "
let message = text + stockCode!  // runtime error

There will be no compile-time error. The compiler assumes that the optional contains a value as forced unwrapping is used. When you run the app, a runtime error is thrown with the following message:

fatal error: Can’t unwrap Optional.None

Huh?!! Then what’s the point?

We’re far enough along in 2018 that a language could have come with forced linting. The compile-time error should be, “Use of a force unwrap on an optional without a nil check is not allowed.”

I’ll admit I’m glad Swift exists. I thought it’d be fun to research SpriteKit and hack up a quick 2D game to relax, and Objective-C looks a little bit more of a head rethread than my usual [stolen] line that “all programming languages are just dialects of the same language”.

But it’s a pain that Swift’s let means the opposite of what it does, so to speak, in JavaScript, and this “Hey, we force one level of null checks in the language, but then reintroduce the ability to have run-time null errors just like you had before,” doesn’t seem really well thought out.

Linters ftw, imo.