[Issue 17896] Alternate version of std.conv.to which returns Nullable

d-bugmail at puremagic.com d-bugmail at puremagic.com
Fri Oct 13 15:34:44 UTC 2017


https://issues.dlang.org/show_bug.cgi?id=17896

--- Comment #3 from Steven Schveighoffer <schveiguy at yahoo.com> ---
Anything that uses opCast is circumventing to's builtin mechanisms anyway as to
defers to the type "It knows better than me". It's no different here.

struct S1
{
   string s;
   auto opCast(T : Nullable!int)() { return customizedConversion(); }
}

struct S2
{
   string s;
   auto opCast(T : int)() { return customizedConversion(); } // can throw
}

void main()
{
   S1 s1;
   S2 s2;
   auto x = s1.to!(Nullable!int); // s1.opCast!(Nullable!int)
   auto y = s2.to!(Nullable!int); // Nullable!int _tmp; try { _tmp =
s2.opCast!int; } catch {}; y = _tmp;
}

And of course, normal builtins cast to Nullable!T should not do a throw and
catch, but optimize that out.

--


More information about the Digitalmars-d-bugs mailing list