4 ms·
This "modern" style of string formatting might seem pretty convenient and concise, but in my opinion, it has quite a few drawbacks. I'd prefer something that
by stephc_int13 5y ago
This "modern" style of string formatting might seem pretty convenient and concise, but in my opinion, it has quite a
few drawbacks.
I'd prefer something that would maybe be less concise but easier to read and maintain, using the host language instead of a mini script using in-band signaling and its weird syntax.
- thomasahle 5y agoIMO having a mini language for strong formating makes much more sense than trying to force those capabilities into the host language.
- ChrisSD 5y ago`printf` isn't exactly modern!
- VWWHFSfQ 5y agomaybe they just don't like string formatting at all? I'm not sure what they mean.
- stillicidious 5y agoDefinitely concur. In a well designed language, I'd expect it to be possible to express something a bit closer to C++'s ios but without the crazy verbosity, and without switching mode. It's not that general language features like this aren't possible, it's often that they simply haven't been discovered yet. User-defined literals are a recent concept that helps eliminate some crap in a related area. Meanwhile, can't complain all that much if zero-cost features can be added to Rust to make it easier to market it to scripting folk. I think that can only be a good thing, even if the feature design is far from ideal.
- wongarsu 5y agoI'm not sure what kind of alternative you are imagining. The style's I'm aware of are "Hello {username}!" "Hello $username!" "Hello " . username . "!" "Hello " + username + "!" "Hello " << username << "!" Of those, I find the first and second one by far the easiest to read, and the first one is easier to extend (as rust has done, e.g. "USD{total:>6}" for a left-padded number).
- Cthulhu_ 5y agoThese are all forms of string interpolation and concatenation, but the 'go to' way is using string formatting, e.g. `printf("Hello %s", username)` (or `sprintf` to just return the resulting string instead of print it to stdout). It doesn't add additional syntax (and therefore complexity, compiler steps, build time, etc) to the language; the only convenience applied here is varargs for the arguments besides the first. A lot of languages started with basic (s)printf and added string interpolation later on, but that brings its own headaches. I'm thinking of PHP, where on the one hand you can do "hello $username", but if it's a property in an object you need to add additional syntax already - "hello {$user->name}".
- deleted 5y ago[deleted]
- wongarsu 5y agoRust already does that. Just that sprintf("Hello %s", username) is written format!("Hello {}", username), which made it easy to now extend it to allow format!("Hello {username}"). Being a late comer made it easy to plan ahead for the future. But really I didn't really mention it because printf syntax seems to me like exactly the "mini script using in-band signaling and its weird syntax" that GP complained about.
- dhosek 5y agoAnd it's important to note that format (and its relatives) is a macro and not a function. That means that whatever the input syntax is for format, whether you write format!("Hello {}", username) or format!("Hello {username}") it will compile as (something like) "Hello ".to_string() + username (as an aside, I really appreciate the fact that Rust macros are syntactically distinct entities so that you can tell at a glance whether you're doing a function call or something potentially strange is happening by way of a macro). In contrast, the old-school stdio sprintf (and relatives) will interpret the format string at run time and then read a varargs list to do the interpolation which can lead to run-time errors and buffer-overflow vulnerabilities and so forth.
- deleted 5y ago[deleted]