-1

A few weeks ago, I went on Udemy course on Swift, the instructor mentioned that constants are called let in Swift because it's standard in OOP (JavaScript uses it for example).

I know this may seem like a really stupid question, but I think it is confusing that constants are declared by let. As, let doesn't stand for constant.

Where did the let keyword come from?

13
  • 3
    Do you have an example of a language or some code where let is used specifically for constants?
    – Useless
    Commented Jan 19, 2021 at 16:07
  • 2
    What language are you talking about? The far most popular one that uses let to declare variables is Javascript, but in Javascript a variable declared with let is not in fact a constant. Commented Jan 19, 2021 at 16:10
  • Swift another example. Commented Jan 19, 2021 at 16:27
  • I was told in a tutorial that let is common convention. Also why the -1? Commented Jan 19, 2021 at 16:28
  • 1
    Let being for a variable predates Javascript - see the original Dartmouth BASIC so predates any OOP language
    – mmmmmm
    Commented Jan 20, 2021 at 15:20

3 Answers 3

6

Using the keyword let to declare constants is not a general convention. The keyword let is used that way in Swift, Rust and Haskell, but in Javascript it denotes a regular variable (just one with a special scope).

As for where the use of let to declare variables come from, it's a very old tradition for mathematical proofs:

A proof in group theory using lots of "let"s

(Source, due to popular demand: https://www.maths.tcd.ie/~dwilkins/LaTeXPrimer/Theorems.html)

0
1

The use of let for a constant is simply a combination of two factors:

  • The use of let for variables (as Michael Borgwardt explained)
  • The belief that variables should be immutable (constants) unless specified otherwise.

In JavaScript, let declares a variable which is mutable (not a constant).
In Haskell, let declares a variable which is immutable. That's because all variables in Haskell are constant.
In Rust, let declares an immutable variable and let mut declares a mutable variable. This is because the designers of Rust wanted to make variables immutable by default - they wanted you to take extra time to think "do I really need mut here?" in the belief that this will reduce the number of bugs you create.

1
  • 2
    Let is common throughout functional programming languages, including the ML family (SML, Ocaml, Haskell) and the Lisp family starting with Scheme. It's definitely not common in OOP languages though, despite what OP's instructor alleges.
    – amon
    Commented Jan 19, 2021 at 21:53
1

Well, if you want to go (koff, koff ...) much earlier than that, in the original Dartmouth BASIC language, "everything had to begin with a verb." So, originally, it had to be LET A=4. But it didn't take very long for people to realize that the grammar could just as easily be coded to make the keyword optional.

But – since that time – various subsequent language-designers have picked-up on that same word, and unfortunately they have used it in many and inconsistent ways. "So it goes," I guess.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.