Tom Ritchford
1 min readJul 11, 2021

--

Suppose you wanted to count down from n to 1:

for (unsigned i = n; n; i--)

(Would I do that? No — I'd use the condition n > 0 for readability, anticipating the compiler would optimize away the compare, and not really caring if it did or didn't. But if I saw the above code snippet, I wouldn’t think worse of the writer…)

Both Go and Zig have the right solution here: Make the length of the array part of the type and introduce slices that bundle their length as a way of writing algorithms for variable length arrays.

This is against one of the key tenets of C — you don't pay for what you don't use.

Your idea would double the cost of passing arrays around, as you have to pass two pieces of data instead of one.

C++ has two good solutions to this. If you know the length of the array at compile time, you use std::array where the compiler keeps track of the length. If you don't, you use std::vector.

The idea of starting with C and trying to make a modern language by tweaking it is a bit silly. C++ is already that language and it’s already somewhat out-of-date.

For a new language, we should either be starting from scratch, or starting from C++ and learning from its mistakes (like Rust did).

--

--

Responses (1)