Make enum auto castable

Mike B Johnson via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sun Jun 4 18:30:47 PDT 2017


On Monday, 5 June 2017 at 00:51:15 UTC, Jonathan M Davis wrote:
> On Monday, June 05, 2017 00:16:15 Mike B Johnson via 
> Digitalmars-d-learn wrote:
>> On Sunday, 4 June 2017 at 23:39:11 UTC, Jonathan M Davis wrote:
>> > [...]
>>
>> I might be able to change the enum, I assume you mean 
>> something like
>>
>> enum myenum : S { }
>>
>> where S is the struct that implicitly converts?
>
> Yes.
>
> However, be aware that you can currently only define one alias 
> this per type. So, the rest of the code will then need to be 
> able to deal with the fact that the enum is a struct that 
> implicitly converts to VARIANT and does not implicitly convert 
> to int. So, if you're just passing the enums around and 
> comparing them, you're fine, but if you need to treat them as 
> ints somewhere, then you'll need to provide an explicit 
> conversion (via overloading opCast or by creating a specific 
> function for it or just exposing a member with the int value or 
> whatever), and that could get annoying in the same way that 
> you're annoyed about the VARIANT issue right now.
>
> But if you don't actually need to treat the enum as an int, and 
> you can make it a struct that implicitly converts to VARIANT 
> instead, then that will fix your VARIANT conversion problem.
>
> - Jonathan M Davis



enum X : EnumX
{
    a = 1,
}


struct EnumX
{
	int x;
	alias x this;
	void opAssign(int y)
	{
		x = y;
	}
	double opCall()
	{
             return x;
	}
}

doesn't work because "1" is not castable to EnumX, even though 
EnumX is aliased to an int, and hence it should work fine.

Seems like a bug to me.



More information about the Digitalmars-d-learn mailing list