3 ms·
> nullptr, null-terminated strings Any system that lacks them will have to hack them back in at some point, in one form or another. Option<types> are not an o
by jstimpfle 4y ago
> nullptr, null-terminated strings
Any system that lacks them will have to hack them back in at some point, in one form or another.
Option<types> are not an option for C, and sentinels like NULL work just fine.
- forrestthewoods 4y ago> Any system that lacks them will have to hack them back in at some point, in one form or another. Only because the C FFI is the lingua franca of operating systems. > Option<types> are not an option for C Disagree. C could absolutely support SumTypes / TaggedUnions. > sentinels like NULL work just fine. Spans/Slices are objectively superior. Null terminated strings aren't the biggest source of pain in the universe, but they're problematic. I'm not arguing that C _should_ add these things today. I'm saying that C made bad choices 50 years ago and we're paying the price to this day. This isn't pointing fingers. It'd be upsetting if the industry hadn't made progress and didn't learn better ways to do things!
- jstimpfle 4y ago> Only because the C FFI is the lingua franca of operating systems. No, because they are genuinely useful. How should you best implement Option<type> for pointers? Right, using NULL as a sentinel. And representing sum types explicitly in the type system (instead of ad-hoc using sentinels) would add a huge amount of complexity. I still like the simplicity of zero-terminated strings. They are self-contained strings that can be passed around as a simple char-pointer. No other format can give you this simplicity. They are still an acceptable default for string literals. And whenever you have a non-trivial usecase, just use the best string representation for that. You don't need to use zero-terminated strings everywhere. Use a macro like this for a dumb length-delineated string struct MyString { const char *ptr; int length; }; #define S(stringlit) (struct MyString) { "" slit, sizeof slit - 1 }; Done!