www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Postfix string literal alternative suggestion

reply Edward Diener <eddielee_no_spam_here tropicsoft.com> writes:
The string literal in D can have a postfix character of 'c', 'w', or 'd' 
for specifying the type of the literal. This notation is in the same 
spirit as the C++ prefix of L"..." to specify a wide character string. 
Such syntax does not play well with templates based on a character type.

I would like to suggest instead the use of a cast(type) expression for 
string literals, perhaps something like 'string_cast(type)"some string 
literal"' where the type would have to be char, wchar, or dchar. This 
says to treat the literal as a particular type and would be a 
replacement for "some string literal"c|w|d notation. The reason I think 
my suggestion is superior is that one may have a template class or 
function in which one of the template parameters is a character type and 
then one can easily specify the template type using the notation 
suggested by me to coerce a string literal to the preferred type without 
having to know the type of the template parameter.
Feb 09 2008
next sibling parent "Jarrett Billingsley" <kb3ctd2 yahoo.com> writes:
"Edward Diener" <eddielee_no_spam_here tropicsoft.com> wrote in message 
news:fokmmc$1bpe$1 digitalmars.com...
 The string literal in D can have a postfix character of 'c', 'w', or 'd' 
 for specifying the type of the literal. This notation is in the same 
 spirit as the C++ prefix of L"..." to specify a wide character string. 
 Such syntax does not play well with templates based on a character type.

 I would like to suggest instead the use of a cast(type) expression for 
 string literals, perhaps something like 'string_cast(type)"some string 
 literal"' where the type would have to be char, wchar, or dchar. This says 
 to treat the literal as a particular type and would be a replacement for 
 "some string literal"c|w|d notation. The reason I think my suggestion is 
 superior is that one may have a template class or function in which one of 
 the template parameters is a character type and then one can easily 
 specify the template type using the notation suggested by me to coerce a 
 string literal to the preferred type without having to know the type of 
 the template parameter.

What? Mind providing some example code?
Feb 09 2008
prev sibling next sibling parent reply "Janice Caron" <caron800 googlemail.com> writes:
On 09/02/2008, Edward Diener <eddielee_no_spam_here tropicsoft.com> wrote:
 then one can easily specify the template type using the notation
 suggested by me to coerce a string literal to the preferred type without
 having to know the type of the template parameter.

But you can do that anyway void f(C)() { invariant(C)[] s = "hello world"; /*...*/ } The literal will be coerced, and s will be of the correct type. No suffix is needed because this is an assignment statement. You would need a suffix in an arbitrary expression, but you don't need one in an assignment statement, so all you have to do is break your code up a bit more.
Feb 09 2008
parent Edward Diener <eddielee_no_spam_here tropicsoft.com> writes:
Janice Caron wrote:
 On 09/02/2008, Edward Diener <eddielee_no_spam_here tropicsoft.com> wrote:
 then one can easily specify the template type using the notation
 suggested by me to coerce a string literal to the preferred type without
 having to know the type of the template parameter.

But you can do that anyway void f(C)() { invariant(C)[] s = "hello world"; /*...*/ } The literal will be coerced, and s will be of the correct type. No suffix is needed because this is an assignment statement. You would need a suffix in an arbitrary expression, but you don't need one in an assignment statement, so all you have to do is break your code up a bit more.

Good example. Thanks ! Still I would have preferred not to have to take this roundabout approach if possible and coerce the literal directly to the type C.
Feb 09 2008
prev sibling parent reply Walter Bright <newshound1 digitalmars.com> writes:
Edward Diener wrote:
 The string literal in D can have a postfix character of 'c', 'w', or 'd' 
 for specifying the type of the literal. This notation is in the same 
 spirit as the C++ prefix of L"..." to specify a wide character string. 
 Such syntax does not play well with templates based on a character type.
 
 I would like to suggest instead the use of a cast(type) expression for 
 string literals, perhaps something like 'string_cast(type)"some string 
 literal"' where the type would have to be char, wchar, or dchar. This 
 says to treat the literal as a particular type and would be a 
 replacement for "some string literal"c|w|d notation. The reason I think 
 my suggestion is superior is that one may have a template class or 
 function in which one of the template parameters is a character type and 
 then one can easily specify the template type using the notation 
 suggested by me to coerce a string literal to the preferred type without 
 having to know the type of the template parameter.
 

You can already use the syntax: cast(string)"foo"; cast(wstring)"bar"; cast(dstring)"abc"; if you need to.
Feb 09 2008
next sibling parent reply "Janice Caron" <caron800 googlemail.com> writes:
On 09/02/2008, Walter Bright <newshound1 digitalmars.com> wrote:
 You can already use the syntax:

         cast(string)"foo";
         cast(wstring)"bar";
         cast(dstring)"abc";

 if you need to.

I did not know that. That seems counterintuitive. I would not expect a simple cast to perform UTF conversion. (I would have expected "bar" and "abc" both to have type invariant(char)[3]).
Feb 09 2008
parent reply Derek Parnell <derek psych.ward> writes:
On Sat, 9 Feb 2008 21:28:29 +0000, Janice Caron wrote:

 On 09/02/2008, Walter Bright <newshound1 digitalmars.com> wrote:
 You can already use the syntax:

         cast(string)"foo";
         cast(wstring)"bar";
         cast(dstring)"abc";

 if you need to.

I did not know that. That seems counterintuitive. I would not expect a simple cast to perform UTF conversion. (I would have expected "bar" and "abc" both to have type invariant(char)[3]).

The 'cast' form does not do UTF conversion. It only works with literals and it tells the compiler which format to store the literal in. If you use cast with variables, no UTF conversion is performed instead it tells the compiler to pretend that the bits are not what the datatype says. dstring ds; ... cast(char[])ds // Pretend that 'ds' really is a char[]. It is annoying that that sometimes 'cast' means convert and sometimes it means pretend and sometimes it means something else. -- Derek Parnell Melbourne, Australia skype: derek.j.parnell
Feb 09 2008
parent reply bearophile <bearophileHUGS lycos.com> writes:
Derek Parnell:
 It is annoying that that sometimes 'cast' means convert and sometimes it
 means pretend and sometimes it means something else.

I agree. I think the many casts you can find in C++ aren't much easy to use & remember, but the alternative may have many disadvantages too. I think this situation has to be improved (probably splitting the semantics to different syntaxes). Bye, bearophile
Feb 09 2008
parent reply Don Clugston <dac nospam.com.au> writes:
bearophile wrote:
 Derek Parnell:
 It is annoying that that sometimes 'cast' means convert and sometimes it
 means pretend and sometimes it means something else.

I agree. I think the many casts you can find in C++ aren't much easy to use & remember, but the alternative may have many disadvantages too. I think this situation

 (probably splitting the semantics to different syntaxes).

C++ hasn't got the distinction right either. Even reinterpret_cast<> sometimes does conversion rather than 'pretend'. Sometimes it makes a difference to generated code. double x = float.min*float.epsilon; x/=3.0; float y = 3.0f * pretendcast(float)x; float z = 3.0f * conversioncast(float)x; --> y is float.min*float.epsilon z is 0. For DMD, cast(float) is a pretend cast, not a conversion cast. But a conversion cast also makes sense, and could be useful.
 
 Bye,
 bearophile

Feb 12 2008
parent Derek Parnell <derek psych.ward> writes:
On Tue, 12 Feb 2008 13:54:24 +0100, Don Clugston wrote:

It would be sort of nice to do something like ..
 
 double x = float.min*float.epsilon;
 
 x/=3.0;
 
 float y = 3.0f * assume(float)x;
 float z = 3.0f * convert(float)x;
 


-- 
Derek Parnell
Melbourne, Australia
skype: derek.j.parnell
Feb 12 2008
prev sibling parent Edward Diener <eddielee_no_spam_here tropicsoft.com> writes:
Walter Bright wrote:
 Edward Diener wrote:
 The string literal in D can have a postfix character of 'c', 'w', or 
 'd' for specifying the type of the literal. This notation is in the 
 same spirit as the C++ prefix of L"..." to specify a wide character 
 string. Such syntax does not play well with templates based on a 
 character type.

 I would like to suggest instead the use of a cast(type) expression for 
 string literals, perhaps something like 'string_cast(type)"some string 
 literal"' where the type would have to be char, wchar, or dchar. This 
 says to treat the literal as a particular type and would be a 
 replacement for "some string literal"c|w|d notation. The reason I 
 think my suggestion is superior is that one may have a template class 
 or function in which one of the template parameters is a character 
 type and then one can easily specify the template type using the 
 notation suggested by me to coerce a string literal to the preferred 
 type without having to know the type of the template parameter.

You can already use the syntax: cast(string)"foo"; cast(wstring)"bar"; cast(dstring)"abc"; if you need to.

I would not have guessed that, but that is great since it could be used directly in templates where the type is a template paramater. I take back my suggestion since you have anticipated it.
Feb 09 2008