Thread overview | ||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
March 11, 2004 Interpolation in strings | ||||
---|---|---|---|---|
| ||||
Hello, I miss the interpolation of variables inside strings, such as the one found in Perl. For example: "I've got $n items" works exactly like: "I've got " ~ n ~ " items" This simply makes things shorter and more readable. The question is what expressions should be allowed. Perl interpolates only variable values and indexing of arrays and hashes. I think that allowing object member access (using '.') would be good, as long as no space is allowed around the '.'. E.g. "First element of a is $a[0]" "foo.bar=$foo.bar" Or, wrap any expression in brackets, e.g. "a+b=${a+b}" I would prefer the interpolation to be done by default inside every double-quoted string. If you want the dollar sign, use \$. This shouldn't be a problem in practice, since the interpolation is syntax-checked at compile-time. Well, that certainly needs discussion, because it's an important feature. |
March 11, 2004 Re: Interpolation in strings | ||||
---|---|---|---|---|
| ||||
Posted in reply to Piotr Fusik | Piotr Fusik wrote:
> Hello,
>
> I miss the interpolation of variables inside strings, such as the one found in
> Perl.
> For example:
> "I've got $n items"
> works exactly like:
> "I've got " ~ n ~ " items"
>
> This simply makes things shorter and more readable.
>
> The question is what expressions should be allowed. Perl interpolates only
> variable values and indexing of arrays and hashes. I think that allowing object
> member access (using '.') would be good, as long as no space is allowed around
> the '.'. E.g.
> "First element of a is $a[0]"
> "foo.bar=$foo.bar"
> Or, wrap any expression in brackets, e.g.
> "a+b=${a+b}"
>
> I would prefer the interpolation to be done by default inside every
> double-quoted string. If you want the dollar sign, use \$. This shouldn't be a
> problem in practice, since the interpolation is syntax-checked at compile-time.
>
> Well, that certainly needs discussion, because it's an important feature.
>
>
int n = 300; // For lack of a better number
char[] aDString;
aDString = "I've got " ~ n.toString() ~ " items.";
// Are there toStrings on primitives?
Alternatively you could make a printf() like function to insert variables into string and return a string.
At any rate, it's *not* that difficult, leaves you with a more verbose and organized code where you can immediatly spot the variable being inserted into the string.
Cheers,
Sigbjørn Lund Olsen
|
March 11, 2004 Re: Interpolation in strings | ||||
---|---|---|---|---|
| ||||
Posted in reply to Piotr Fusik | Piotr Fusik wrote: > Hello, > > I miss the interpolation of variables inside strings, such as the one found in > Perl. > For example: > "I've got $n items" > works exactly like: > "I've got " ~ n ~ " items" > > This simply makes things shorter and more readable. Shorter: yes. More readable: not to my eyes, but I haven't worked with Perl. My biggest criticism to this idea is that it would make it harder to parse strings. It would be more work for those writing D compilers, tools to analyze D code, syntax highlighting tools, etc. I think the only language that I've written in that allows this is PHP (which I just started learning a few weeks ago). I'm not used to this concept and I'm not convinced that it's particularly helpful. I'd likely have a different view if I'd used it for a long time. I think it's an interesting idea, but I'm afraid it would enable new exotic problems. -- Justin http://jcc_7.tripod.com/d/ |
March 12, 2004 Re: Interpolation in strings | ||||
---|---|---|---|---|
| ||||
Posted in reply to J C Calvarese | J C Calvarese wrote:
> Piotr Fusik wrote:
>
>> Hello,
>>
>> I miss the interpolation of variables inside strings, such as the one found in
>> Perl.
>> For example:
>> "I've got $n items"
>> works exactly like:
>> "I've got " ~ n ~ " items"
>>
>> This simply makes things shorter and more readable.
>
> Shorter: yes.
> More readable: not to my eyes, but I haven't worked with Perl.
>
>
> My biggest criticism to this idea is that it would make it harder to parse strings. It would be more work for those writing D compilers, tools to analyze D code, syntax highlighting tools, etc.
>
> I think the only language that I've written in that allows this is PHP (which I just started learning a few weeks ago). I'm not used to this concept and I'm not convinced that it's particularly helpful. I'd likely have a different view if I'd used it for a long time.
>
> I think it's an interesting idea, but I'm afraid it would enable new exotic problems.
>
In PHP it's helpful until you realize that due to this 'liberal' design and other pieces of 'liberal' design it could compromise your code's security. It's better now that register_globals is off by default, but still not completely safe. While it makes a whole lot of sense to be very careful and not trust any input in a publically available script, it's surprising how many PHP coders actually do, and leave their scripts wide open to various forms of SQL (or other) insertion attacks. Also any 'client' can switch the dreaded register_globals back on, and surprisingly many idiots (including webspace providers & ISPs) populate this planet and do. As a consequence I *never* use "" to delimit strings anymore (nor should anyone), effectively forcing myself to use the concatenation operator when I want to insert a variable into a string. Of course, there's more checking to be done since it's a weakly typed language, etc etc etc.
PHP isn't really a very good language. Like Perl and other scripting languages it seems to have been designed with 'getting the job done FAST' first in mind, and everything, including resource use, speed, *security* and more last.
Cheers,
Sigbjørn Lund Olsen
|
March 12, 2004 Re: Interpolation in strings | ||||
---|---|---|---|---|
| ||||
Posted in reply to Piotr Fusik | Piotr Fusik wrote: > Hello, > > I miss the interpolation of variables inside strings, such as the one found in > Perl. > For example: > "I've got $n items" <snip> If you want Perl, you know where to find it. This 'group is about D, a language in which quotation marks have an established purpose: to delimit a string _literal_. Stewart. -- My e-mail is valid but not my primary mailbox, aside from its being the unfortunate victim of intensive mail-bombing at the moment. Please keep replies on the 'group where everyone may benefit. |
March 12, 2004 Re: Interpolation in strings | ||||
---|---|---|---|---|
| ||||
Posted in reply to Sigbjørn Lund Olsen | Sigbjørn Lund Olsen wrote: <snip> > Alternatively you could make a printf() like function to insert variables into string and return a string. <snip> Given a means of passing forward the dreaded variable argument list, it would be straightforward to make a wrapper for sprintf to do this. As long as you have an upper bound for the length of the resulting string. But when D finally gets its own decent I/O system, it would probably extend to working with strings. See thread "Possible solution for D's I/O system" from 2-3 Mar, especially my contribution. Stewart. -- My e-mail is valid but not my primary mailbox, aside from its being the unfortunate victim of intensive mail-bombing at the moment. Please keep replies on the 'group where everyone may benefit. |
March 12, 2004 Re: Interpolation in strings | ||||
---|---|---|---|---|
| ||||
Posted in reply to Sigbjørn Lund Olsen | "Sigbjørn Lund Olsen" <sigbjorn@lundolsen.net> wrote in message news:c2rugj$29j$1@digitaldaemon.com... > In PHP it's helpful until you realize that due to this 'liberal' design and other pieces of 'liberal' design it could compromise your code's security. It's better now that register_globals is off by default, but still not completely safe. While it makes a whole lot of sense to be very careful and not trust any input in a publically available script, it's surprising how many PHP coders actually do, and leave their scripts wide open to various forms of SQL (or other) insertion attacks. Also any 'client' can switch the dreaded register_globals back on, and surprisingly many idiots (including webspace providers & ISPs) populate this planet and do. As a consequence I *never* use "" to delimit strings anymore (nor should anyone), effectively forcing myself to use the concatenation operator when I want to insert a variable into a string. Of course, there's more checking to be done since it's a weakly typed language, etc etc etc. This does sound like a disastrous problem, but since D strings would not be interpreted at runtime, it shouldn't be a problem. Embedding $n is an interesting idea. I don't know if it adds enough utility, though. |
March 12, 2004 Re: Interpolation in strings | ||||
---|---|---|---|---|
| ||||
Posted in reply to Walter | Walter wrote: > "Sigbjørn Lund Olsen" <sigbjorn@lundolsen.net> wrote in message > news:c2rugj$29j$1@digitaldaemon.com... > >>In PHP it's helpful until you realize that due to this 'liberal' design >>and other pieces of 'liberal' design it could compromise your code's >>security. It's better now that register_globals is off by default, but >>still not completely safe. While it makes a whole lot of sense to be >>very careful and not trust any input in a publically available script, >>it's surprising how many PHP coders actually do, and leave their scripts >>wide open to various forms of SQL (or other) insertion attacks. Also any >>'client' can switch the dreaded register_globals back on, and >>surprisingly many idiots (including webspace providers & ISPs) populate >>this planet and do. As a consequence I *never* use "" to delimit strings >>anymore (nor should anyone), effectively forcing myself to use the >>concatenation operator when I want to insert a variable into a string. >>Of course, there's more checking to be done since it's a weakly typed >>language, etc etc etc. > > > This does sound like a disastrous problem, but since D strings would not be > interpreted at runtime, it shouldn't be a problem. Why wouldn't they be interpreted at runtime? After all he's talking about inserting variables. I realize it's just syntactic sugar of the sort: "$a is a variable inserted into this string." meaning a.toString ~ " is a variable inserted into this string." as it is now (and personally I'm perfectly happy with that, even though I quite like C++'s insertion operators which hides the toString so to speak in normal usage)... You can't use concatenation to implicitly call toString like Java's println will, though, because it'll be ambiguous due to array and string concatenation being the same thing. Does the programmer intend for the integer to be converted to a string representation, or kept in its binary form when concatenating? I doubt the compiler would know, unless there was another operator, and I'm guessing we don't want nor need two operators for concatenation. But it'd certainly be interpreted at runtime. I agree, that it's unlikely to cause any problems in D, simply because a lot of the dodgy runtime framework of PHP isn't present. To sum it up, really, the problem with PHP is as follows: First and most importantly, PHP is so simple that most people writing PHP code are idiots, who do not consider for a second that they are writing executable code that will run in the use of serving web pages - a publically available and accessible spot. Most PHP coders seem to trust benevolent use, which is ridiculous for code that is run on request by anybody on the internet. PHP does not require initialization of variables before use. PHP is also loosely typed, so type conversion is always implicit. The register_globals setting, while now off by default, is still on (for backwards compatability and ease of coding) in many installations. For those who don't know it, register_globals creates variables named after HTTP and ENV data, so that http://www.foobar.com/?authorized=1 would make a variable $authorized which evaluates to boolean true. But essentially, in PHP if register_globals is on, any client can create variables with arbitrary names and content. The eval() function takes a string and runs it as if it were code. Most PHP scripts interface with databases for data retrieval and storage, using SQL. All this compiles into a nice slush where ignorant programmers create scripts for public invocation, where anyone can create a variable of arbitrary content, name and type, which is then inserted into strings (by said incompetent programmers) no questions asked, and then expedited into an eval() or an SQL query. Lamentable results ensue. Luckily, all of that has nothing to do with D. > Embedding $n is an interesting idea. I don't know if it adds enough utility, > though. Well, *I* don't think so. Having different ways of doing the same thing is all very nice, but to a limit. Having $n mean 'concatenate surrounding string(s) with variable n' just adds another character we need to escape in literal strings and makes it harder to follow the flow of the code when skimming through someone elses code. And all of that for the sake of not having to type out " ~ n.toString ~ "... If the programmer resents typing so much he could even make himself a utility function template (this might not compile, never done much of templates, but I think you'll get the gist of it): template ToString(T) { char[] s(T variable) { return T.toString(); } } + some aliases for instantiating this with specific types... or a void* function or somesuch that will allow you to do something like this: s(var) ~ " is a variable put into this string." *looks at the above argument and code* I really ought to go to bed, oughtn't I? Cheers, Sigbjørn Lund Olsen |
March 12, 2004 Re: Interpolation in strings | ||||
---|---|---|---|---|
| ||||
Posted in reply to Stewart Gordon | Stewart Gordon wrote: > Piotr Fusik wrote: > >> Hello, >> >> I miss the interpolation of variables inside strings, such as the one found in >> Perl. >> For example: >> "I've got $n items" > > <snip> > > If you want Perl, you know where to find it. And if Perl has a killer feature that D could benefit from I think we should let someone suggest it. It's not like he's endlessly extolling the virtues of that paragon of programming languages, C#. (Ominous music plays...) > > This 'group is about D, a language in which quotation marks have an established purpose: to delimit a string _literal_. He's making a suggestion for an improvement to D. He's not asking us to join a Perl fan club. While I think that the benefits don't outweigh the costs of the proposal, I still think it's still an interesting idea. I'm glad it was suggested. > > Stewart. -- Justin http://jcc_7.tripod.com/d/ |
March 13, 2004 Re: Interpolation in strings | ||||
---|---|---|---|---|
| ||||
Posted in reply to J C Calvarese | >He's making a suggestion for an improvement to D. He's not asking us to join a Perl fan club.
Thanks. That's exactly what I meant.
I see that there was much misunderstanding here: the feature should be treated
just as a syntactic sugar! It does not make D a scripting language.
If you don't get "syntactic sugar": you can write ++i instead of (i=i+1), even
though both are semantically the same.
I looked at sources written in C, C++, Java, C#, Perl, D, Python... and I think
that saving some characters is really worth trouble.
Compare this:
"x=$x y=$y z=$z"
with this:
"x="~x~" y="~y~" z="~z
Which one is easier to write and read?
|
Copyright © 1999-2021 by the D Language Foundation