Jump to page: 1 2
Thread overview
Postfix string literal alternative suggestion
Feb 09, 2008
Edward Diener
Feb 09, 2008
Janice Caron
Feb 09, 2008
Edward Diener
Feb 09, 2008
Walter Bright
Feb 09, 2008
Janice Caron
Feb 09, 2008
Derek Parnell
Feb 09, 2008
bearophile
Feb 12, 2008
Don Clugston
Feb 12, 2008
Derek Parnell
Feb 09, 2008
Edward Diener
February 09, 2008
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.

February 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?


February 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.
February 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.
February 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]).
February 09, 2008
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
February 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
February 09, 2008
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.
February 09, 2008
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.
February 12, 2008
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 has to be improved
> (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
« First   ‹ Prev
1 2