std.conv: conversion from ulong to enum

H. S. Teoh hsteoh at quickfur.ath.cx
Thu Aug 22 18:29:20 PDT 2013


On Thu, Aug 22, 2013 at 05:00:38PM -0700, Andrei Alexandrescu wrote:
> On 8/22/13 5:00 PM, Andrei Alexandrescu wrote:
> >On 8/22/13 3:48 PM, Matej Nanut wrote:
> >>Hi everyone,
> >>
> >>I hope I'm not misunderstanding the spec when this won't compile:
> >>
> >>---
> >>import std.conv;
> >>enum Test { a = 0 }
> >>void main() {
> >>     ulong l = 0;
> >>     auto t = l.to!Test;
> >>}
> >>---
> >>
> >>It doesn't compile due to the template not instantiating.
> >>
> >>If l is an int, uint or byte, it compiles fine.  I'm assuming the fault
> >>lies in the ‘is(S : OriginalType!T)’ constraint.
> >>
> >>So, um, is this intended behaviour?  And why are string enums not
> >>supported? Wouldn't the ‘==’ operator work just fine on them too (so the
> >>function body would stay the same)?
> >>
> >>Thanks,
> >>Matej
> >
> >Hrm, that's a bug. Could you please submit to d.puremagic.com/issues?
> >
> >Thanks,
> >
> >Andrei
> 
> Never mind, I see we track it already.
[...]

Actually, I just fixed issue 6893:

	https://github.com/D-Programming-Language/phobos/pull/1504

but the OP's code still doesn't work. This modified version does,
though:

	import std.conv;
	enum Test : ulong { a = 0 }
	void main() {
		ulong l = 0;
		auto t = l.to!Test;
	}

The reason is that ulong doesn't implicitly convert to int, which is the
default base type of enum. Whether to() *should* attempt the conversion
is a different matter, though. I think we should still file a separate
bug for this (issue 6893 is a similar bug with the same ulong > int
conversion issue, but the fix doesn't address this particular problem).


T

-- 
Computers are like a jungle: they have monitor lizards, rams, mice, c-moss, binary trees... and bugs.


More information about the Digitalmars-d mailing list