3 ms·
> Contrast this with a language like C, in which string operations are (unless the programmer writes or uses a library) far more explicit String operations are
by humanrebar 12y ago
> Contrast this with a language like C, in which string operations are (unless the programmer writes or uses a library) far more explicit
String operations are more explicit. Mostly.
But some things (like sharing between threads, type conversion, and memory ownership) are very implicit and unsafe. Some of the implicitness to C programmers is so familiar that they don't notice it:
/* 1. This function returns an error code
* and not a size
* 2. zero is success, nonzero is an error */
int
/* 3. this method belongs in mylib
* 4. this function supports ASCII???
* 5. this function allows memory to overlap? */
mylib_copy_first_n(
/* 6. *out_s is allocated
* 7. *out_s is greater than n bytes in size
* 8. out_s will hold a null terminated string */
char const * out_s,
/* 9. in_s is allocated
* 10. *in_s holds at least n characters
* 11. it's not a big deal if in_s has null characters */
char const * in_s,
/* 10. n is not negative
* 11. if n is zero, something sane will happen */
int n);
I wouldn't hold up C as an example of explicitness. Now, this isn't a great example of C code, but even in the best examples of C code, there is a lot of correctness by convention and documentation.
To be fair, I'm not aware of any languages and libraries that are fully explicit in type, behavior, and memory usage. Maybe Ada or one of the functional languages get it right. But there are a set of C++ tools that can make this sort of thing fast and correct. That's why it's disappointing that std::string isn't fast and is only mostly correct.
- pjmlp 12y agomylib: DEFINITIONS = BEGIN copy_first_n: PROCEDURE [VAR out_s: STRING, in_s: STRING, n: CARDINAL ] RETURNS [error: BOOLEAN]; END Mesa at Xerox PARC - 1979, one of the influences for strong type systems programming languages. But the 80's startups had to go build workstations based on UNIX.
- asveikau 12y agoNitpick: I don't think the output buffer would be const.