4 ms·
> Pascal had the correct format for strings, the one you mentioned and the one used by all modern languages. Not really. The most crucial insight is that your
by tialaramex 4y ago
> Pascal had the correct format for strings, the one you mentioned and the one used by all modern languages.
Not really. The most crucial insight is that your string references should be fat pointers, which is not what Pascal does. Look at Rust's &str for the state of the art. Other choices are less important, but having string references be fat pointers is a huge boon. It would probably have seemed profligate in the 1980s, but it was the right choice.
- mananaysiempre 4y ago> Look at Rust's &str for the state of the art. Other choices are less important, but having string references be fat pointers is a huge boon. It would probably have seemed profligate in the 1980s, but it was the right choice. ANS Forth[1], released 1994, used (addr, len) pairs throughout, eliminating most uses of “counted” (Pascal) strings in Forth-83 and earlier. [1] https://forth.sourceforge.net/std/dpans/ https://forth.sourceforge.net/std/dpans/
- Gibbon1 4y agoC with fat pointers and boxing/unboxing would be far more super duper than people realize. The other three are first class types, tagged unions, and closures. Standards committee says no.
- kevin_thibedeau 4y agoThat would be a mess for interop with 8, 16, and 32-bit targets. What size should the length be? What happens when you exceed it? Nowadays you can just 32-bit all the things and ignore most range issues. That luxury didnt exist in the 80s.
- tialaramex 4y ago> That would be a mess for interop with 8, 16, and 32-bit targets. What size should the length be? What happens when you exceed it? It works fine. The length has the same size as an address, the fat pointer being a pair of addresses is technically equivalent but has worse performance in practice so you should make it an (address, length) pair instead of course. You can't exceed this length because of the pigeonhole principle, basically just arithmetic.
- mananaysiempre 4y ago> The length has the same size as an address, the fat pointer being a pair of addresses is technically equivalent but has worse performance in practice so you should make it an (address, length) pair instead of course. Is it worse though? Serious question, I’ve been wondering for quite a bit. IME processing a string from the start is easier with a (start, end) pair (one operation instead of two when you chop off things from the start). You can’t use those in standard C because it reserves the right to blow up on pointer subtraction essentially randomly (ptrdiff_t overflow is UB, and the only requirement for ptrdiff_t is that it hold 2^15-1; C89 doesn’t even tell you what the maximum allowed value is), but that’s not a performance problem, and not a problem at all in a different language.