Live data from Hacker News

John Carmack on mutable variables

twitter.com

661–663 of 663 posts

Re: John Carmack on mutable variables

#661
post #263

Earlier quoted context omitted.

1st) you use ++def in a loop, don't be weird; 2nd) if 'abc' is to be used in the loop body, define in the loop, e.g. for (int def = 7, abc =3; ...); 3rd) this is an IntelliJ bug - both 'def' and 'abc' in the sample are always defined.

> you use ++def in a loop, don't be weird I come from a C++ background where it is always advised to use ++i instead of i++. It's just a habit. Does it stress you to read ++i over i++?

>Does it stress you to read ++i over i++?

Of course not, I do use both. Admittedly i++ is a lot more common in Java, and for loops i++ is the standard idiom. Not using the standard idiom usually implies less practice, e.g. str.indexOf('x')

  for (int i = array.length; i-- > 0;) doStuff(array[i]);

Re: John Carmack on mutable variables

#662

Earlier quoted context omitted.

I can give a specific example. for (0..5) |i| { i = i + 1; std.debug.print("foo {}\n", .{i}); } In this loop in Zig, the reassignment to i fails, because i is a constant. However, i is a new constant bound to a different value each iteration. To potentially make it clearer that this is not mutation of a constant between iterations, technically &i could change between iterations, and the program would still be correct…

I argue in your example there are 6 constants, not 1 constant with 6 different values, though this could be semantics ie we could both be right in some way

Exactly. As said higher in this comment chain:

> So, constant, but repeatedly redefined.

Re: John Carmack on mutable variables

#663

Earlier quoted context omitted.

I argue in your example there are 6 constants, not 1 constant with 6 different values, though this could be semantics ie we could both be right in some way

Exactly. As said higher in this comment chain: > So, constant, but repeatedly redefined.

it was constant if it could have been redefined, by the basic definition of what constant means. so no, not constant, but constants
Post reply on HN