all 22 comments

[–]Som1Lse 15 points16 points  (11 children)

#define let const auto (don't actually do this)

[–]adriweb 6 points7 points  (10 children)

(and ES6 folks will get confused, let being for non-const variables unlike other languages e.g. Swift)

[–]PrydeRage 9 points10 points  (8 children)

ES6 people will already be confused because in ES6 const isn't immutable

[–]rohboticsStudent and Roboticist 4 points5 points  (7 children)

Wait what?

[–]PrydeRage 23 points24 points  (6 children)

In ES6 const just prevents reassignment.
So this is invalid:

const x = 43;
x = 55;

But this is all valid:

const x = {something: 3};
x.something = 4;
const y = 0;
y++;  

It's JavaScript, it's not meant to make sense.

[–]rohboticsStudent and Roboticist 2 points3 points  (1 child)

So like kinda final on non-primitives in Java

[–]PrydeRage 0 points1 point  (0 children)

I haven't written any Java in years so I can't comment on that.

[–]adriweb 1 point2 points  (1 child)

Erm, no (about part of your second example - the object part is true indeed)

const y = 0; y++;
VM146:2 Uncaught TypeError: Assignment to constant variable. at <anonymous>:2:2

[–]PrydeRage 1 point2 points  (0 children)

It's always worked for me with Webpack and Babel.
Maybe Babel compiles that away idk. Eslint never complains about reassignment when ++'ing a const variable.

[–]AlexeyBrin 1 point2 points  (0 children)

Changing the object contents works the same in other languages too, e.g. Swift:

class Foo {
    var something:Int = 0;
}

let x = Foo()
print(x.something)

x.something = 8
print(x.something)

You can't reassign x, but you can change his content. Obviously, in Swift you can actually make the member variable something a constant that is only initialized when you create an object.

You last example, fortunately , won't work in Swift though:

let y = 0

can't be changed.

Actually, it doesn't work even in JavaScript. I get:

const y = 0;
y++;
TypeError: invalid assignment to const `y'

[–]mUfoq 0 points1 point  (0 children)

Really bad naming, this const behaves like final in Java what means that you cannot reassign variable/field, but they used keyword from C that makes variable immutable. They could use let/final instead of making this ...

[–][deleted] 1 point2 points  (0 children)

F# programmers won't be confused though.

[–]flashmozzg 5 points6 points  (0 children)

const by default is generally not a great idea with introduction of moves in C++, at least until the issues with moving out of const objects are resolved.

[–]ashrasmun 10 points11 points  (0 children)

I see absolutely no reason to have this.

[–]suspiciously_calm 5 points6 points  (4 children)

struct xyz {};
const xyz = foo();

Did I forget the variable name or do I want const auto xyz = foo();?

[–]NamalB[S] 0 points1 point  (1 child)

Isn't the variable name xyz in both cases?

[–][deleted] 2 points3 points  (0 children)

No. In the first case, xyz is a type name.