November 15, 2011 Re: enum for beginners | ||||
---|---|---|---|---|
| ||||
Posted in reply to Mike Wey | On 15/11/2011 20:37, Mike Wey wrote:
> On 11/14/2011 11:25 PM, Johannes Totz wrote:
>> Hi!
>>
>> I'm having trouble with named typed enums.
>> This works (unnamed):
>>
>> enum : string
>> {
>> a = "a",
>> b = "b"
>> }
>>
>> int main(string[] argv)
>> {
>> writeln(a);
>> return 0;
>> }
>>
>>
>> But this does not:
>>
>> enum X : string
>> {
>> a = "a", // Error: Integer constant expression expected
>> // instead of "a"
>> b = "b" // Error: Integer constant expression expected
>> // instead of "b"
>> }
>>
>> int main(string[] argv)
>> {
>> writeln(X.a);
>> return 0;
>> }
>>
>>
>> What did I miss?
>>
>>
>> Johannes
>
> It's a bug: string enums don't work with -g compiler switch.
>
> http://d.puremagic.com/issues/show_bug.cgi?id=5168
Ah thanks! I've added my case...
How would i go about using a custom class for the enum-type?
Various combinations of the snippet below lead to errors.
class EnumType
{
int x;
alias x this;
this(int i)
{
x = i;
}
void opCall(int i)
{
}
}
enum X : EnumType
{
a = EnumType(1),
b = 2
}
Errors range are always variations of
main.d(16): Error: function main.EnumType.opCall need 'this' to access member opCall
main.d(23): called from here: opCall(1)
main.d(23): Error: cannot implicitly convert expression (opCall(1)) of type void to main.EnumType
main.d(24): Error: cannot implicitly convert expression (2) of type int to main.EnumType
|
November 15, 2011 Re: enum for beginners | ||||
---|---|---|---|---|
| ||||
Posted in reply to Johannes Totz | Johannes Totz:
> class EnumType
> {
> int x;
> alias x this;
>
> this(int i)
> {
> x = i;
> }
>
> void opCall(int i)
> {
> }
> }
>
> enum X : EnumType
> {
> a = EnumType(1),
> b = 2
> }
>
>
> Errors range are always variations of
>
> main.d(16): Error: function main.EnumType.opCall need 'this' to access member opCall
The error message suggests you to use a static opCall.
And are you sure you want a class instead of a struct?
Bye,
bearophile
|
November 15, 2011 Re: enum for beginners | ||||
---|---|---|---|---|
| ||||
Posted in reply to bearophile | On 15/11/2011 22:56, bearophile wrote:
> Johannes Totz:
>
>> class EnumType
>> {
>> int x;
>> alias x this;
>>
>> this(int i)
>> {
>> x = i;
>> }
>>
>> void opCall(int i)
>> {
>> }
>> }
>>
>> enum X : EnumType
>> {
>> a = EnumType(1),
>> b = 2
>> }
>>
>>
>> Errors range are always variations of
>>
>> main.d(16): Error: function main.EnumType.opCall need 'this' to access
>> member opCall
>
> The error message suggests you to use a static opCall.
> And are you sure you want a class instead of a struct?
struct works, thanks!
No need for class.
|
Copyright © 1999-2021 by the D Language Foundation