3 ms·
While this might not be a popular observation around here, about all the design choices that have led us from C to C++20 (and soon enough C++23) have been speci
by ncmncm 6y ago
While this might not be a popular observation around here, about all the design choices that have led us from C to C++20 (and soon enough C++23) have been specifically to address problems of this nature. (Rust may someday be as versatile, but that is not on the immediate agenda.)
There is really no reason ever to even consider restricting yourself to C when coding for a microcontroller. The ability to do computation at compile time is essential in this area, and C is just fundamentally lacking. While in principle you could do your compile-time work in the makefile, instead, complicating your build process rarely turns out well.
Some people insist C++ is about "O-O" notions of inheritance, virtual functions, and heap allocation; others, that it is about STL and std::vector. All such people are dead, dead wrong. You don't (generally!) use any of that in microcontroller coding, yet you routinely draw upon all the most powerful features of the language and library to make code that is maximally short, fast, small, and correct. These features put the type system to work for you, not just checking but actively making code correct.
Debugging on microcontrollers is hard enough with a language that makes bad code easier to write than good code. In modern C++ (as now also in Rust), when the program builds and links, it is more often than not right. The more you help yourself to correctness with powerful language features, the more often this happens.
While Zig has features to do computation at compile time, it lacks features to help make that computation correct. Such features would depend on a more powerful type system than Zig provides. Debugging bad compile-time computation at runtime, in a microcontroller, is no fun.
- dnautics 6y ago> While Zig has features to do computation at compile time, it lacks features to help make that computation correct. Huh? Zig will reject comptime overflows/underflows, and you can write suites of inline tests too if you'd like, so whatever you miss at comptime you can constrain using runtime CI-validation at the location of interest.
- ncmncm 6y agoAs written immediately above, "Such features would depend on a more powerful type system than Zig provides." So Zig offers as much help as it can, without.