Thread overview
Append wchar enumeration to string
Nov 21, 2010
Nrgyzer
Nov 21, 2010
Simen kjaeraas
Nov 21, 2010
Nrgzyer
Nov 21, 2010
Simen kjaeraas
Nov 21, 2010
Nrgyzer
Nov 21, 2010
Nrgyzer
November 21, 2010
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.
November 21, 2010
Nrgyzer <nrgyzer@gmail.com> wrote:

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

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 = "$",
	EURO = "€",
	YEN = ...

}

-- 
Simen
November 21, 2010
When I use an string enumeration, I get the following error (dmd1):

CURRENCY base type must be of integral type, not char[]
November 21, 2010
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
November 21, 2010
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...".
November 21, 2010
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.
November 22, 2010
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