TypeFunction example creatiing a conversion matrix

H. S. Teoh hsteoh at quickfur.ath.cx
Thu Oct 1 18:51:44 UTC 2020


On Thu, Oct 01, 2020 at 02:28:37PM -0400, Steven Schveighoffer via Digitalmars-d wrote:
> On 10/1/20 2:19 PM, H. S. Teoh wrote:
[...]
> > We would like to be able to do this:
> > 
> > 	Variant v;
> > 	v = 100; // v now stores an int
> > 	long l = v.get!long; // currently does not work
> 
> I'm not going to respond to anything else here, but just want to point
> out, this does work.
> 
> https://run.dlang.io/is/S3sqDF

OK, I didn't check before I posted, :-/ but the point is that the
implicit conversion is manually hacked with a library template that does
not cover all the cases.  We should not have to reimplement implicit
conversions in library code when the compiler is already perfectly
capable of doing it.

It's not just implicit conversions, though.  There's a host of other
things that we cannot do currently.  Like conversion to string: in
theory, since std.conv.to is able to convert just about any type to
string, we ought to be able to support v.get!string for *any* stored
type.  Yet we cannot because at runtime all we have is typeid(T), so we
cannot dispatch to the correct overload of std.conv.to to create the
string.


T

-- 
Being forced to write comments actually improves code, because it is easier to fix a crock than to explain it. -- G. Steele 


More information about the Digitalmars-d mailing list