2 ms·
C (unlike e.g. Zig) doesn't have any builtin 'struct types' (not sure about VLAs though, but those had been a dead end anyway). Adding such types into the langu
by flohofwoe 4y ago
C (unlike e.g. Zig) doesn't have any builtin 'struct types' (not sure about VLAs though, but those had been a dead end anyway). Adding such types into the language would be quite a change in philosophy.
- LiamPowell 4y agostruct tm in time.h has been around since C89, there may be others but that's all that immediately comes to mind.
- flohofwoe 4y agoBut these are stdlib features, not language features. "Proper" strings, arrays, slices, tagged unions, etc... would need to be integrated into the language (unless you want to end up with a mess like C++).
- uecker 4y agoI am exploring way on how to implement this in C (with some extensions) as a library: https://github.com/uecker/noplate https://github.com/uecker/noplate Certainly not production ready.
- pjmlp 4y agoNot really, something like sds for strings would be doable. For arrays you may be right, possibly a fat pointer like suggested by Denis himself.
- uecker 4y agoVLA are exactly an array type that knows its length, i.e. a dependent type. VLAs on the stack had some issues, but pointers to VLAs, i.e. variably modified types are quite powerful. Language and compiler support is still evolving though. But you can get run-time bounds checking already: https://godbolt.org/z/jhcavobYj https://godbolt.org/z/jhcavobYj Support for statically detecting problems is also (slowely) improving.
- nicoburns 4y agoSeems to me that a struct isn't really any philosophically different to say a float. The only difference it that it's larger.
- flohofwoe 4y agoOne problem I can imagine is that builtin struct types might have implications for ABIs. The C standard probably would prefer to not define their interior and memory layout for portability, but these things need to be clearly defined for ABIs. For instance in a slice type, does this have a start and end pointer, or a start pointer and size? Does the pointer or size come first? Is size the number of elements, or number of bytes? Alignment? Padding? Etc...
- _kst_ 4y agoC certainly does have struct types. Apparently you mean something by "struct types" other than the well known C feature. Can you elaborate?
- flohofwoe 4y agoEmphasis on 'builtin' :) For example, Zig's slice type is a 'builtin struct' which contains a pointer and size, the builtin error union type is made of an error code and the actual result type. These things are not provided by the Zig standard libary, but instead built into the language. In contrast, C's builtin types are all 'primitives' (pointers, integers and floats) - with the arguable exception of VLAs, which have been made non-mandatory in C11.
- uecker 4y agoWe made VLA types and derived types (variably modified types) mandatory in C23. (but not VLAs on the stack as automatic variables which are still optional)
- gpderetta 4y ago_Complex
- flohofwoe 4y agoOops you're right, I never understood why complex numbers ended up as a language feature or are even in the stdlib, but anyway, thanks for the correction.