www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Append wchar enumeration to string

reply Nrgyzer <nrgyzer gmail.com> writes:
Hello guys,

I have an enumeration which contains symbols of different currencies
like the following:

enum CURRENCY : wchar {

	DOLLAR = '$',
	EURO = '¤',
	YEN = ...

}

When I try to append it to a string like

char[] myString = "Currency: " ~ CURRENCY.DOLLAR

I get the following error: "Error: incompatible types for... 'char[]'
and 'int'"

But... when I try the following:

char[] myString = "Currency: " ~ cast(char) CURRENCY.DOLLAR

It works, but not for all currencies.
What can I do to support all currencies?

Thanks for any help.
Nov 21 2010
next sibling parent reply "Simen kjaeraas" <simen.kjaras gmail.com> writes:
Nrgyzer <nrgyzer gmail.com> wrote:

 But... when I try the following:

 char[] myString =3D "Currency: " ~ cast(char) CURRENCY.DOLLAR

 It works, but not for all currencies.
 What can I do to support all currencies?
The problem is that not all UTF-8 code points fit in one char. I would recommend in this situation to use a string enum: enum CURRENCY : string { DOLLAR =3D "$", EURO =3D "=E2=82=AC", YEN =3D ... } -- = Simen
Nov 21 2010
parent reply Nrgzyer <nrgzyer gmail.com> writes:
When I use an string enumeration, I get the following error (dmd1):

CURRENCY base type must be of integral type, not char[]
Nov 21 2010
parent reply "Simen kjaeraas" <simen.kjaras gmail.com> writes:
Nrgzyer <nrgzyer gmail.com> wrote:

 When I use an string enumeration, I get the following error (dmd1):

 CURRENCY base type must be of integral type, not char[]
Ah, you're using D1. Well, a solution would be to use dchar[] instead of char[]. -- Simen
Nov 21 2010
parent reply Nrgyzer <nrgyzer gmail.com> writes:
But for enums I can't use char[] as basetype and when I use cast(dchar)
or cast(wchar) I also get "Error: incompatible types for...".
Nov 21 2010
parent Nrgyzer <nrgyzer gmail.com> writes:
I solved the problem by using toUTF8 from std.utf but I think it's a
dirty solution because I have to cast wchar to wchar[] because a
simple toUTF8(CURRENCY.DOLLAR) matches wchar[] and dchar[] signatures.
Nov 21 2010
prev sibling parent "Steven Schveighoffer" <schveiguy yahoo.com> writes:
On Sun, 21 Nov 2010 05:58:30 -0500, Nrgyzer <nrgyzer gmail.com> wrote:

 Hello guys,

 I have an enumeration which contains symbols of different currencies
 like the following:

 enum CURRENCY : wchar {

 	DOLLAR = '$',
 	EURO = '�',
 	YEN = ...

 }

 When I try to append it to a string like

 char[] myString = "Currency: " ~ CURRENCY.DOLLAR

 I get the following error: "Error: incompatible types for... 'char[]'
 and 'int'"
I'm guessing from the code/error, this is D1. D2 does support appending different width characters to dchar[], I'm not sure about appending wchar to a char[] though. I don't think D1 has such support. It also doesn't have support for enums as strings, so I think you may just have to create a function that returns a char[], given a wchar. -Steve
Nov 22 2010