implicit conversion

Jonathan M Davis via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Tue Aug 12 07:26:26 PDT 2014


On Tue, 12 Aug 2014 13:17:37 +0000
Meta via Digitalmars-d-learn <digitalmars-d-learn at puremagic.com> wrote:

> On Tuesday, 12 August 2014 at 06:37:45 UTC, Jonathan M Davis via
> Digitalmars-d-learn wrote:
> > The problem is that isNaN is now templatized, and its
> > constraint uses
> > isFloatingPoint, which requires that the type _be_ a floating
> > point type, not
> > that it implicitly convert to one. So, as it stands, isNAN
> > cannot work with
> > any type which implicitly converts to a floating point value.
> > Either it will
> > have to be instantiated with the floating point type - e.g.
> > isNaN!float(s) -
> > or you're going to have to explicitly cast s to a floating
> > point type.
> >
> > You can open a bug report - https://issues.dlang.org - and mark
> > it as a
> > regression, and it might get changed, but the reality of the
> > matter is that
> > templates don't tend to play well with implicit conversions.
> > It's _far_ too
> > easy to allow something in due to an implicit conversion and
> > then have it not
> > actually work, because the value is never actually converted.
> > In general, I
> > would strongly advise against attempting to give types implicit
> > conversions
> > precisely because they tend to not play nicely with templates.
> >
> > - Jonathan M Davis
>
> I think this should be considered a bug. A type with alias this
> should work in all cases that the aliased type would. If the
> function fails to be instantiated with S!float, then it should be
> forwarded to the S's val member.

AFAIK, the only time that the implicit conversion would take place is when the
type is being used in a situation where it doesn't work directly but where the
aliased type is used. In that case, the compiler sees the accepted types and
sees that the type can implicitly convert to one of the accepted types and
thus does the conversion. So, it knows that the conversion will work before it
even does it. The compiler never attempts to do the conversion just to see
whether it will work, which is essentially what it would have to do when
attempting to use the type with a templated function. You can certainly create
an enhancement request for such behavior, but I have no idea how likely it is
get implemented. There are currently _no_ cases where the compiler does
anything with template instantiations to try and make them pass if simply
trying to instantiate them with the given type failed.

- Jonathan M Davis


More information about the Digitalmars-d-learn mailing list