D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 10874 - std.conv.to should support conversion from ulong to int-based enum
Summary: std.conv.to should support conversion from ulong to int-based enum
Status: RESOLVED FIXED
Alias: None
Product: D
Classification: Unclassified
Component: phobos (show other issues)
Version: D2
Hardware: All All
: P2 normal
Assignee: No Owner
URL:
Keywords: pull
Depends on:
Blocks:
 
Reported: 2013-08-22 18:54 UTC by hsteoh
Modified: 2013-08-24 02:37 UTC (History)
1 user (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description hsteoh 2013-08-22 18:54:49 UTC
Code:
------
import std.conv;
enum Test { a = 0 }
void main() {
    ulong l = 0;
    auto t = l.to!Test;
}
------

DMD output:

/usr/src/d/phobos/std/conv.d(281): Error: template std.conv.toImpl does not match any function template declaration. Candidates are:
/usr/src/d/phobos/std/conv.d(337):        std.conv.toImpl(T, S)(S value) if (isImplicitlyConvertible!(S, T) && !isEnumStrToStr!(S, T) && !isNullToStr!(S, T))
/usr/src/d/phobos/std/conv.d(449):        std.conv.toImpl(T, S)(ref S s) if (isRawStaticArray!S)
/usr/src/d/phobos/std/conv.d(465):        std.conv.toImpl(T, S)(S value) if (is(typeof(S.init.opCast!T()) : T) && !isExactSomeString!T)
/usr/src/d/phobos/std/conv.d(496):        std.conv.toImpl(T, S)(S value) if (!isImplicitlyConvertible!(S, T) && is(T == struct) && is(typeof(T(value))))
/usr/src/d/phobos/std/conv.d(546):        std.conv.toImpl(T, S)(S value) if (!isImplicitlyConvertible!(S, T) && is(T == class) && is(typeof(new T(value))))
/usr/src/d/phobos/std/conv.d(281):        ... (16 more, -v to show) ...
/usr/src/d/phobos/std/conv.d(281): Error: template std.conv.toImpl(T, S)(S value) if (isImplicitlyConvertible!(S, T) && !isEnumStrToStr!(S, T) && !isNullToStr!(S, T)) cannot deduce template function from argument types !(Test)(ulong)
/usr/src/d/phobos/std/conv.d(281): Error: template instance toImpl!(Test) errors instantiating template
test.d(5): Error: template instance std.conv.to!(Test).to!(ulong) error instantiating
Comment 2 github-bugzilla 2013-08-24 02:33:44 UTC
Commits pushed to master at https://github.com/D-Programming-Language/phobos

https://github.com/D-Programming-Language/phobos/commit/8536853fc86632883d6ad18096b4980bacbb5056
Fix issue 10874.

Don't assume that is(A : B) means a==b is valid, or that !is(A : B)
implies that a==b is invalid. Instead, test for a==b explicitly.

More specifically, int and ulong are ==-comparable, even though ulong is
not implicitly convertible to int. Thus, one should be able to convert
ulong to an int-based enum, but is(A : B) in the signature constraint
prohibits this. Testing for a==b explicitly, OTOH, makes this work.

https://github.com/D-Programming-Language/phobos/commit/a184d6b484c3f2968c379f48c6fff42828b98375
Merge pull request #1505 from quickfur/issue10874

Issue 10874 - conv.to ulong to int enum conversion