Issue 18339 - Variant.coerce is unable to convert between types that std.conv.to is able to convert
Summary: Variant.coerce is unable to convert between types that std.conv.to is able to...
Status: NEW
Alias: None
Product: D
Classification: Unclassified
Component: phobos (show other issues)
Version: D2
Hardware: All All
: P4 enhancement
Assignee: No Owner
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2018-01-30 21:56 UTC by hsteoh
Modified: 2024-12-01 16:32 UTC (History)
0 users

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description hsteoh 2018-01-30 21:56:01 UTC
Code:
----------
import std.conv;
import std.variant;

struct MyValue {
        int impl;
        auto opCast(T : MyOtherValue)() {
                return MyOtherValue(impl);
        }
}

struct MyOtherValue {
        int impl;
}

void main() {
        auto mv = MyValue(123);
        MyOtherValue mov2 = mv.to!MyOtherValue; // OK

        Variant v = mv;
        MyOtherValue mov = v.coerce!MyOtherValue; // NG
}
----------

Compiler output:
----------
/usr/src/d/phobos/std/variant.d(844): Error: static assert:  "unsupported type for coercion"
----------

Given the current implementation of Variant, I'm not sure how possible this is, but it would be *very* nice if there were some way of saying, given an object of type Variant, "I don't care what the actual underlying type is, just convert it to T using std.conv.to or throw an exception if that's not possible".  Currently, you have to know that the stored type is MyValue, then you can extract a MyValue and convert it to MyOtherValue.  However, in generic code, it's often not possible (or desirable) to have to know what the original type is; sometimes the code just knows it wants some desired type T, and Variant should either convert to T somehow (preferably with std.conv.to, since that's the standard conversion tool in D), or throw an exception if the underlying types are incompatible.

Related: issue #12997.
Comment 1 dlangBugzillaToGithub 2024-12-01 16:32:21 UTC
THIS ISSUE HAS BEEN MOVED TO GITHUB

https://github.com/dlang/phobos/issues/10301

DO NOT COMMENT HERE ANYMORE, NOBODY WILL SEE IT, THIS ISSUE HAS BEEN MOVED TO GITHUB