www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Small idea: javascript-like string constants

reply "Lionello Lunesu" <lio remove.lunesu.com> writes:
Just a small idea, haven't really thought it fully through yet:

Since D has no zero-terminated strings, a character constant is basically 
the same as a string constant: 'a' and "a" are basically the same.

This opens up the possibility of using ' and " similar to javascript (or 
python for that matter), being able to mix the two: 'blah "var"' would be 
the same as the current "blah \"var\"".

While writing this the multi byte character constants come to mind. I do 
code similar to this in some C++ prog:

long c = (long&)file_extension;
switch (c)
{
case 'gepj':// jpeg
case 'gpj'://jpg
case 'agt'://tga
    // it's an image file
}

I'll still post this though, out of curiousity for the thoughts of the 
group.

L. 
Dec 13 2005
next sibling parent reply =?ISO-8859-1?Q?Anders_F_Bj=F6rklund?= <afb algonet.se> writes:
Lionello Lunesu wrote:

 Since D has no zero-terminated strings, a character constant is basically 
 the same as a string constant: 'a' and "a" are basically the same.

D has length-prefixed strings instead, so I don't think that's correct ? You can switch on strings in D, though... http://www.digitalmars.com/d/statement.html#switch --anders
Dec 13 2005
parent reply "Lionello Lunesu" <lio remove.lunesu.com> writes:
"Anders F Björklund" <afb algonet.se> wrote in message 
news:dnmcnb$rob$1 digitaldaemon.com...
 Lionello Lunesu wrote:

 Since D has no zero-terminated strings, a character constant is basically 
 the same as a string constant: 'a' and "a" are basically the same.

D has length-prefixed strings instead, so I don't think that's correct ?

Well, ok, the array has a pointer to the actual value together with the length: char[1] b = "a"; //points to an 'a' in memory, paired with length 1 Whereas in C "a" and 'a' would have nothing in common: the former being a pointer to {97,0} and the latter being just the value 97. In D it's no longer to think of "a" as 'a pointer to a string', eventhough arrays contain a pointer to the string. I think of "a" as being a collection of characters, this one having only 'a'. Is a collection with only 'a' different from just 'a'? How is a char[1] different from a char?
 You can switch on strings in D, though...
 http://www.digitalmars.com/d/statement.html#switch

Which makes the strange (and not portable) switch construct in my original post obsolete! L.
Dec 13 2005
parent reply =?ISO-8859-1?Q?Anders_F_Bj=F6rklund?= <afb algonet.se> writes:
Lionello Lunesu wrote:

 Whereas in C "a" and 'a' would have nothing in common: the former being a 
 pointer to {97,0} and the latter being just the value 97.

Pretty much in common, yes ? i.e. char[0] == char
 In D it's no longer to think of "a" as 'a pointer to a string',
 eventhough arrays contain a pointer to the string. I think of "a" as
 being a collection of characters, this one having only 'a'. Is a
 collection with only 'a' different from just 'a'?

Yes. One is a collection, the other is an item. :-)
 How is a char[1] different from a char?

It isn't. But null-terminated arrays and length-prefixed arrays are also very similar. Don't get me wrong, I also like D's "code unit" arrays better than C's "character" arrays... But they are not really *that* different from eachother ? And you can use raw D strings for things with double-quotes: `blah "var"` The only downside being that in shell scripts and other languages, the backticks means to escape to a sub-process... --anders
Dec 13 2005
parent "Lionello Lunesu" <lio remove.lunesu.com> writes:
 And you can use raw D strings for things with double-quotes:
 `blah "var"`

THAT I didn't know :-) Thanks for pointing it out. L.
Dec 13 2005
prev sibling parent reply Hasan Aljudy <hasan.aljudy gmail.com> writes:
Lionello Lunesu wrote:
 Just a small idea, haven't really thought it fully through yet:
 
 Since D has no zero-terminated strings, a character constant is basically 
 the same as a string constant: 'a' and "a" are basically the same.

Well it's not, as been shown in another post.
 
 This opens up the possibility of using ' and " similar to javascript (or 
 python for that matter), being able to mix the two: 'blah "var"' would be 
 the same as the current "blah \"var\"".

in D, you can use `blah "quote" etc` (that is not the single quote ' it's the thing in the ~ button)
 
 While writing this the multi byte character constants come to mind. I do 
 code similar to this in some C++ prog:
 
 long c = (long&)file_extension;
 switch (c)
 {
 case 'gepj':// jpeg
 case 'gpj'://jpg
 case 'agt'://tga
     // it's an image file
 }

You can do switch on strings in D
Dec 13 2005
parent reply Stewart Gordon <smjg_1998 yahoo.com> writes:
Hasan Aljudy wrote:
<snip>
 in D, you can use
 `blah "quote" etc`
 (that is not the single quote ' it's the thing in the ~ button)

Have you checked that Lionello's keyboard is exactly the same as yours? Stewart. -- -----BEGIN GEEK CODE BLOCK----- Version: 3.1 GCS/M d- s:- C++ a->--- UB P+ L E W++ N+++ o K- w++ O? M V? PS- PE- Y? PGP- t- 5? X? R b DI? D G e++>++++ h-- r-- !y ------END GEEK CODE BLOCK------ My e-mail is valid but not my primary mailbox. Please keep replies on the 'group where everyone may benefit.
Dec 14 2005
next sibling parent reply =?ISO-8859-1?Q?Anders_F_Bj=F6rklund?= <afb algonet.se> writes:
Stewart Gordon wrote:

 (that is not the single quote ' it's the thing in the ~ button)

Have you checked that Lionello's keyboard is exactly the same as yours?

On my keyboard, it (`) is right next to the backspace key. And I don't have a ~ key either... Just us non-US weirdos. :-) --anders PS. Backticks are still dumb to use for the raw strings, regardless... (since they're used elsewhere to run commands. But it's too late)
Dec 14 2005
parent reply Stewart Gordon <smjg_1998 yahoo.com> writes:
Anders F Björklund wrote:
<snip>
 Backticks are still dumb to use for the raw strings, regardless...
 (since they're used elsewhere to run commands. But it's too late)

It shouldn't be surprising that different languages use the same symbol to mean different things. Just to name a few: ^: bitwise XOR in C(++), D, etc. Exponentiation in BASIC, Excel, etc. Beginning of line or 'not' in regexp. Statement separator in 4DOS. $: string variable marker in BASIC. Scalar variable marker in Perl and Unix shell scripts. End of array in D. Mingle operator in INTERCAL. -: subtraction in most languages. Just another character to use in variable names in COBOL. #: preprocessor directive in C(++). Comment in Perl, Unix shell scripts , make and Maple. Double precision (?) variable marker in some BASICs. {}: block statement in C(++), D, etc. Comment in Pascal. Stewart. -- -----BEGIN GEEK CODE BLOCK----- Version: 3.1 GCS/M d- s:- C++ a->--- UB P+ L E W++ N+++ o K- w++ O? M V? PS- PE- Y? PGP- t- 5? X? R b DI? D G e++>++++ h-- r-- !y ------END GEEK CODE BLOCK------ My e-mail is valid but not my primary mailbox. Please keep replies on the 'group where everyone may benefit.
Dec 14 2005
parent =?ISO-8859-1?Q?Anders_F_Bj=F6rklund?= <afb algonet.se> writes:
Stewart Gordon wrote:

 Backticks are still dumb to use for the raw strings, regardless...
 (since they're used elsewhere to run commands. But it's too late)

It shouldn't be surprising that different languages use the same symbol to mean different things. Just to name a few:

Nope, just that this particular one has me confused every single time. Then again it's used once in a blue moon so it's not really a problem. --anders
Dec 14 2005
prev sibling parent "Lionello Lunesu" <lio remove.lunesu.com> writes:
"Stewart Gordon" <smjg_1998 yahoo.com> wrote in message 
news:dnp4ft$tiu$1 digitaldaemon.com...
 Hasan Aljudy wrote:
 <snip>
 in D, you can use
 `blah "quote" etc`
 (that is not the single quote ' it's the thing in the ~ button)

Have you checked that Lionello's keyboard is exactly the same as yours?

It happens to be the same :-) L.
Dec 15 2005