LET it be

About variable assignments

Thomas Künneth
Tommis Programming Blog
3 min readApr 10, 2020

--

Screenshot of the VirtualC64 emulator written by Prof. Dr. Dirk W. Hoffmann
The Commodore 64 user interface

In my previous post about being a polyglot developer I pondered about the usage of arrow constructs in programming languages. This time I will reflect on the let keyword.

Though it may be obvious from other articles I wrote, here is a confession: I love the so-called home computers of the late 70s and early 80s. So, let’s (ah, no pun intended) start with them. The title image, by the way, shows the VirtualC64 emulator by Prof. Dr. Dirk W. Hoffmann. Most (but certainly not all) home computers offered a builtin BASIC interpreter and a (full screen) editor. As BASIC aimed to be easily learnable and understandable, the language looks like short english sentences. For example, to assign a value to a variable, one might say Let A be Hello World. This translates to LET A$ = “HELLO WORLD". The dollar sign is needed to represent sequences of characters. One would use % for integer numbers, as in LET ANSWER% = 42. In most BASIC dialects, LET is optional. So B = 3.14 is valid. Also, re-using LET for assigning values to the same variable is perfectly alright.

So far, so good. But we don’t use BASIC, do we? Other languages use let for assignment, too. If you are interested in its origins, there is an interesting question In what programming language did “let” first appear? on Software Engineering. ES6 brought let to the web. Instead of var we can now say

class black {}
let orange = new black();

The difference between both keywords is scoping. var is scoped to the nearest function block, whereas let is scoped to the nearest enclosing block (which can be smaller than a function block). Both are global if they are outside any block. Variables declared with let are not accessible before they have been declared in their enclosing block. Further assignments to an already declared variable (using let) are valid. Why am I emphasizing this? Let us take a look at Swift. This language knows let, too. However, assignments can be made only once. For non-constant-like usages we need to take var instead.

The Dart programming language is optionally typed. That means that we can either write

var a = 42;
print(a.runtimeType);

or

int a = 42;
print(a.runtimeType);

In both cases int is printed. To declare constants, Dart offers the const keyword:

const a = 42;
print(a.runtimeType);

Again, int is printed. Dart constants are determined during compile-time. So, this one is not valid:

const a = new Random().nextInt(10);

A similar expression in ES6, however, is:

const c2 = Math.random();

The same is true for Swift’s let. The assigned value can (of course) be computed during runtime.

let a = random()
print(a);

Speaking of const, remember that const is a reserved keyword in Java, but currently not used. C#, on the other hand, does:

public const double Pi = 3.14159;

Kotlin uses var to declare re-assignable variables. val is used for one time assignments. Additionally, Kotlin offers the const keyword for top-level compile-time constants:

const val a = 42

Please note that even though const might be enough to mark something as constant and unchangeable, currently you must additionally write val. Before we stop our brain dance for now, take a look at this code snippet:

“Hello”.let { print(“$it, world”)}

Here, let is a scoping function. It takes the object it is invoked upon as the parameter and returns the result of the lambda expression. Variables declared inside the expression cannot be used outside.

Happy coding!

A previous (much less complete) version of this article was published 2017–05–01 on my Blogger.com blog Tommis Blog. I deleted the weblog in the wake of the GDPR (General Data Protection Regulation), so the original post is no longer available (or only through the Wayback machine of the Internet Archive).

If not stated otherwise images are © Thomas Künneth

--

--

Thomas Künneth
Tommis Programming Blog

Google Developer Expert for Android. Author. Speaker. Listener. Loves writing.