LET it be

About variable assignments

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

--

--

--

Short posts about programming

Recommended from Medium

HOW TO GROW YOUR BUSINESS WITH UBEREATS CLONE?

Building React, Flask, and Next.js Web App for Corona Statistics.

January 2020 Review, Google Cloud, Deep Learning Intern, MakerFest, Time Series Forecast, CERN…

What do coders do for a $90k+ salary?

Creating RESTFUL API using Golang and MongoDB

These are the top 5 Basic Tools every developer needs and should master

Basic Toolset

Cross-platform C/C++ development (Android/iOS/Windows/MacOS/Linux)

CSS Grid

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Thomas Künneth

Thomas Künneth

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

More from Medium

Developing a Todo app with android integration testing following TDD approach — Part 3 of 3

Migration of ROOM DB using Auto Migrations

Android vs iOS Which Platform To Build Your App On?

Retain cycles in Swift and Kotlin