Allow declarations of variables without the `var`/`let` keyword

...not to go OT too much, but by "reliably organize" i meant: the ability to group little stanzas of code according to what they do, instead of having Swift force you to group statements by whether or not they mix var, let and function calls with optional s. Doing so improves readability a lot

First post here.

I’d say this is a -1 from me. Swift as a language is focused around safety, explicitly defined variables allow for easy distinction of values that will (or will not) be mutated. Attempting to modify a let constant results in a compiler error, which I believe can often be helpful— having to pause and consider any possible side effects that mutating this variable may have.

I don’t believe this is a particular struggle for those new to the language or even programming itself. This could also cause confusion with scope in some cases.

1 Like

I'm not a fan of this gatekeeping perspective. Programming is still a young profession, and humanity hasn't even really begun to rigorously figure out what complexities are inherent to the medium and not incidental to the tools we happen to use today. Swift does have learnability by new programmers as one of its stated goals, and if even if we don't change the language design, it's still a reasonable goal for the compiler to help people learn the system Swift presents.


:100: I couldn't agree with this more. Every programmer should take this to heart!

Well but in this case time has shown that implicit declaration leads to hard to find errors. So it's bad design.

I'm not arguing for implicit declaration. Like @beccadax is suggesting, we could provide diagnostics that help guide people to using explicit declarations correctly.


At the risk of becoming too philosophical, "learnability" is not the same as "it's immediately obvious to a complete outsider", IMHO, rather that the concept can be taught easily. Anyone who wishes to program but doesn't want to learn anything new is bound to be disappointed. This is how I read jeremyp's post, anyway (and also nick.keets's).

In addition, it seems to me like an unproven assumptions whether variable declarations are really that unintuitive. Maybe beginners think of variables like boxes in which we can put things, and then it would make sense that you have to "define" (i.e. lay out) your box before you put something in it; maybe they don't, who knows. We probably don't have studies to show us whether they are. In any case, such things can be more or less intuitive depending on the background someone has (for example, are they mathematically trained or not).


From my experience with our coder trainees the concept they always stumble is not declaring a variable but to understand what a variable is.

1 Like

Some clarifications. My proposal applies only for when there is a type annotation, not when the type is inferred.

What I'm proposing:

x: Int          // equivalent to: `let x: Int`
x = 100

y: Int = 200    // equivalent to: `let y: Int = 200`

What I'm not proposing:

x = 100
x = 200

y = 200

In the second code snippet it is not clear where the variables are being declared but in the first it is clear.

Since constants are already favored over variables x: Int should only be interpreted as let x: Int.

This contradicts your original post.

This is still a undecided battlefield :slight_smile:

My feeling is that changing the syntax used to declare variables won't really address the issue of potential confusion between declaration and assignment. Having multiple equivalent ways to declare variables will furthermore only increase confusion. I like @beccadax's idea of trying to use diagnostics and fixits to help teach the existing syntax over changing the syntax.


+1 to improved diagnostics
-1 (at least) to the proposal

Readability contributes to learnability. Explicit tends to trump implicit for readability. While it is true that all current programming languages have obfuscatory constructs, whether they be the use of various punctuation characters or unintuitive keywords or the like, Swift is by far the most English-readable language I’ve seen in my 35 years of programming. Much of that is the result of clear thinking about the way methods are named, syntax is laid out, and what keywords are used where. let and var require some knowledge, but it is quickly acquired knowledge, and they increase readability.

1 Like

This will be ambiguous with the existing grammar, or near ambiguous. We already use "foo:" for labels on labeled breaks etc, and colons are used in lots of other places.

We could probably make this work if forced to, but it is a major design smell that this is so similar to existing grammar productions that can exist at the same scope. IMO, something like this would introduce new confusion and complexity, not reduce it.



I would like to add that these lets and vars help the beginners to understand the concept of scope.


Favored by Swift. Encouraged might be a better choice.

I've taught Swift to several hundreds of students (CS and non-CS, teens to elderly) so far and haven't heard a single complaint about the explicit var/let. No student has ever asked my why you need to explicitly declare a constant or variable.

The feedback I've received on this is entirely positive:

  • var/let make it clear that a new constant or variable is being used. Without it, students tend to wonder about this and scroll through their code to double-check.
  • var/let protect against accidental declarations due to typos in a name. Students make a lot of typos. In loosely types languages, typos (like incorrect casing) are probably their #1 source of bugs.
  • var/let make shadowing explicit, intentional and easier to understand. If Swift didn't have explicit declarations, I would probably stop using shadowing all together.

So I don't think there's an issue here.

W.r.t. appealing to scientists: no programming language that uses the equals sign for assignment will ever feel natural to scientists :smiley:


This is not a "gatekeeping perspective", it's a statement of reality.

Programming is still a young profession, and humanity hasn't even really begun to rigorously figure out what complexities are inherent to the medium and not incidental to the tools we happen to use today.

I'm not sure I agree with that, but it's incidental to the point.

With respect to this particular topic, the point was raised that people apparently have an issue with understanding the difference between declaration and assignment and that this is some kind of bar to beginners. I dispute that point: if it is a bar, it is a really low one, certainly much lower than many other important concepts in Swift. I'm sure most people are familiar with the concept of declaring an interest in some resource before using. For example, most hotels will not let you use a room unless you have reserved it in advance. Declaring variables is pretty basic in terms of computer programming. If you can't get it, you are really going to struggle with things like Never and generics and operator precedence and optionals.

All I am saying is that this is not a good argument for doing away with either let or var.


Except Fortran

I agree with “gatekeeping” argument of @Joe_Groff, we should consider that programming has just began forming as science and we should not say “it is hard” for everybody who is trying to start programming, and I think we should improve and develop our knowledge about programming to keep evolution process running.
But there are a huge “but” from me, in which I want fully agree with your statements. I believe that any science and profession requires some basic skills and understanding, like in programming we have declaring and assignment difference, and without them it will be really hard to understand in future. I can compare this like with playing music, notes are hard to understand, but otherwise you won’t be able to play anything.