5 ms·
C had the opportunity to include this but they did not. It is my understanding that they wanted to design everything in C as inherent to the language, rather th
by samtho 3y ago
C had the opportunity to include this but they did not. It is my understanding that they wanted to design everything in C as inherent to the language, rather than magic types, especially a struct. There is an elegance in the notion that a string is just an array of characters. If I’m working with a significant amount of strings in C, I can keep track of lengths, not a huge deal.
- jstimpfle 3y agoExactly this. There are no literals in C that create composite types. There are no composite types inherent to the language. All these types are defined in (system) includes. And zero-terminated strings are not strictly worse than other length-prefixed string forms. They save some space -- sure, less relevant today -- as well as provide an in-band termination signal -- which is hacky, again sure, but it is convenient when looking at a hex dump for example.
- nicoburns 3y ago> There are no literals in C that create composite types Float/double literals do
- jstimpfle 3y agohow so?
- jacquesm 3y agoBecause a float contains bit fields including sign of the exponent, sign, mantissa, sign of the mantissa. It's a bit of a pedantic argument but technically it makes sense.
- jstimpfle 3y agoYou could call them composite in that sense, but in C, composite types are types that are composed of other C types (structs, unions, arrays, ... functions? Not 100% sure of the specifics). Also, the representations of floats and doubles isn't precisely specified, at least IEEE754 is not a strict requirement (not sure about the technical implications from what's actually specified).
- jacquesm 3y agoI'm not the one making the argument, merely explaining why the GGP may have chosen this example.
- jstimpfle 3y agoWasn't implying that! :)
- nicoburns 3y agoI'm not really sure I get why the distinction between composite and non-composite types is important if the only difference is that you can't easily access sub-parts of the non-composite types.
- jstimpfle 3y agoI guess we can cook up some arguments about language purity and orthogonality. Introducing literals that create composite types might indeed create some difficulties for existing compilers that want to follow the latest standard.
- nicoburns 3y agoWhat kind of difficulties? I don't understand why this would make any difference from a compiler's perspective.
- jstimpfle 3y agoIt requires the compiler to be able to create types even before parsing, to have composite types without any source code location... you'd have to check with a specific compiler but it's not hard to imagine some potential issues. There's also outward facing changes that introduce complexity... new literal types (+ new syntax) are required because otherwise old could would break (sizeof...) etc. etc. Existing parsers and tooling will have to be adapted, too... Generally it's nothing that should be insurmountable, but why accept any of that if there's nothing to gain except a feature that is requested mainly by detractors of the ecosystem, a feature that goes squarely against the grain of the language...? Any language is proud to be able to implement its features as library components around its core capabilities. Why introduce a new C type as a structure that can easily be defined as an aggregate type in source code?
- abbeyj 3y agoThere are literals that create composite types, since C99: https://en.cppreference.com/w/c/language/compound_literal https://en.cppreference.com/w/c/language/compound_literal
- jacquesm 3y agoAn early version of C didn't have structs, the initial attempt to get the OS off the ground failed, and after adding structs it worked. Structs are just syntactic sugar over memory offsets relative to a base pointer, a construct for which many CPUs include primitives.
- imtringued 3y agoC is lots of magic and quirkiness. This reminds me. From a spec/design perspective Ceylon was the cleanest language I know. Almost everything, including a lot of the keywords were actually defined in the standard library. The fact that Integer was actually a Java int behind the scenes was just a compiler implementation detail. There was very little "magic". If you wanted to know how something in the language worked you could just look at the standard library.