Comment #0 by bearophile_hugs — 2013-09-22T14:57:28Z
A low priority enhancement request.
String literals support the c w d suffix to specify their type:
void main() {
auto s1 = "hello"c;
auto s2 = "hello"w;
auto s3 = "hello"d;
}
For wchars/dchars you could use a cast:
void main() {
auto c1 = 'X';
auto c2 = cast(wchar)'X';
auto c3 = cast(dchar)'X';
}
But I suggest to support the same string suffixes:
void main() {
auto c1 = 'X';
auto c2 = 'X'w;
static assert(is(typeof(c2) == wchar));
auto c3 = 'X'd;
static assert(is(typeof(c3) == dchar));
}
This has some advantages:
- It's shorter than a cast, it takes only 1 extra char.
- During debugging and in other situations I search for the "cast(" string in my code, because sometimes casts are where bugs are. Removing the need to use cast() for dchars/wchars removes some noise from that search.
- Those suffixes are not hard to learn for a D programmer because they are the same for strings. And I think this change is backwards compatible.
Disadvantages:
- It's not a very common need;
- Unlike the situation with strings where you can't use a cast, the cast(wchar)/cast(dchar) work fine on chars.
One use case:
import std.algorithm: map, joiner;
import std.string: text;
void main() {
string r = [1, 2]
.map!(x => [1, 2].map!(y => cast(dchar)'*'))
.joiner("_")
.text;
}
Comment #1 by bearophile_hugs — 2013-11-10T13:56:31Z
See also this thread:
http://forum.dlang.org/thread/[email protected]
A comment by Jonathan M Davis:
> It certainly sounds like a nice enhancement though to be able to do
>
> 'a'd
>
> especially if the c and w versions gave errors when the character
> wouldn't fit in a char or wchar, which isn't the case with a cast.
Comment #3 by bearophile_hugs — 2013-11-15T04:26:43Z
(In reply to comment #2)
> Well, I implemented it.
>
> It was pretty easy, but I'm really not sure if it's worth doing after all.
>
> https://github.com/yebblies/dmd/tree/issue11103
Thanks to a patch from Kenji we'll be able to write:
import std.algorithm: map, joiner;
import std.string: text;
void main() {
string r = [1, 2]
.map!(x => [1, 2].map!(y => dchar('*')))
.joiner("_")
.text;
}
But having the same string suffixes is shorter, better and more uniform with the string syntax:
.map!(x => [1, 2].map!(y => '*'d))
Comment #4 by andrej.mitrovich — 2014-04-26T19:05:07Z
(In reply to yebblies from comment #2)
> Well, I implemented it.
>
> It was pretty easy, but I'm really not sure if it's worth doing after all.
>
> https://github.com/yebblies/dmd/tree/issue11103
Can we get this as a pull request? It looks like low-hanging fruit to me.
Comment #5 by yebblies — 2014-04-26T19:31:53Z
(In reply to Andrej Mitrovic from comment #4)
>
> Can we get this as a pull request? It looks like low-hanging fruit to me.
If you feel it's worthwhile feel free to take that patch and pull-request it. I can't remember why I didn't go through with it... I'm sure there was some kind of reason. :)
Comment #6 by andrej.mitrovich — 2014-04-26T19:43:51Z
(In reply to yebblies from comment #5)
> (In reply to Andrej Mitrovic from comment #4)
> >
> > Can we get this as a pull request? It looks like low-hanging fruit to me.
>
> If you feel it's worthwhile feel free to take that patch and pull-request
> it. I can't remember why I didn't go through with it... I'm sure there was
> some kind of reason. :)
I'll pass, unicode escapes me. When you have a 680 page book called "Unicode Explained" it's time to say "fuck it." :P
Comment #7 by robert.schadek — 2024-12-13T18:11:49Z