digitalmars.D - Postfix string literal alternative suggestion
- Edward Diener (14/14) Feb 09 2008 The string literal in D can have a postfix character of 'c', 'w', or 'd'...
- Jarrett Billingsley (4/18) Feb 09 2008 What?
- Janice Caron (12/15) Feb 09 2008 But you can do that anyway
- Edward Diener (4/22) Feb 09 2008 Good example. Thanks !
- Walter Bright (6/22) Feb 09 2008 You can already use the syntax:
- Janice Caron (4/9) Feb 09 2008 I did not know that. That seems counterintuitive. I would not expect a
- Derek Parnell (14/26) Feb 09 2008 The 'cast' form does not do UTF conversion. It only works with literals ...
- bearophile (4/6) Feb 09 2008 I agree. I think the many casts you can find in C++ aren't much easy to ...
- Don Clugston (14/24) Feb 12 2008 C++ hasn't got the distinction right either. Even reinterpret_cast<> som...
- Derek Parnell (14/14) Feb 12 2008 On Tue, 12 Feb 2008 13:54:24 +0100, Don Clugston wrote:
- Edward Diener (4/30) Feb 09 2008 I would not have guessed that, but that is great since it could be used
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
"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
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
Janice Caron wrote:On 09/02/2008, Edward Diener <eddielee_no_spam_here tropicsoft.com> wrote: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.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
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
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
On Sat, 9 Feb 2008 21:28:29 +0000, Janice Caron wrote:On 09/02/2008, Walter Bright <newshound1 digitalmars.com> wrote: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.parnellYou 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
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
bearophile wrote:Derek Parnell:has to be improvedIt 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
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
Walter Bright wrote:Edward Diener wrote: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.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