On Saturday, February 24, 2018 04:13:30 psychoticRabbit via Digitalmars-d-
learn wrote:
> On Saturday, 24 February 2018 at 03:58:48 UTC, Jonathan M Davis
> wrote:
> > Whether an implicit cast or an explicit cast makes more sense
> > depends entirely on what the code is doing, but either way, the
> > conversion needs to be forced inside the function, or you end
> > up with bugs. Far too often, when someone has a template
> > constraint that checks an implicit conversion, the function
> > doesn't actually force the conversion, and that can do anything
> > from resulting in some instantiations not compiling to causing
> > subtle bugs due to the argument being used without being
> > converted. In general, it's actually best to avoid conversions
> > entirely with generic code and force the caller to do the
> > conversion if a conversion is appropriate.
> >
> > But ultimately, what works best depends on what the code is
> > trying to do.
> >
> > - Jonathan M Davis
> yeah it's hard to say much more without knowing what the code
> really wants to do..but presumably, you'd want to incorporate
> some contract programming in such a solution too, particulary
> given there's something potentially dodgy going on within such a
> function.

Why is there anything dodgy going on and why would you need contracts?
Contracts actually tend to go very badly with generic code, because whatever
they assert has to be generic, and while that works sometimes, more often
than not, it doesn't.

If you're testing for a conversion in a template constraint, simply forcing
the conversion by assigning it to a variable of the target type (with an
explicit cast if necessary) solves all of the problems related to testing
for a conversion and then writing the code as if the argument were of the
target type rather than a type that converted to the target type.

- Jonathan M Davis

Reply via email to