implicit conversion

Jonathan M Davis via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Tue Aug 12 09:04:41 PDT 2014


On Tuesday, 12 August 2014 at 15:39:09 UTC, Meta wrote:
> What I mean is that this breaks the Liskov Substitution
> Principle, which alias this should obey, as it denotes a 
> subtype.
> Since S!float has an alias this to float, it should behave as a
> float in all circumstances where a float is expected; otherwise,
> we've got a big problem with alias this on our hands.

IMHO, it was a mistake to add alias this to the language. It's
occasionally useful, but it's too dangerous. Implicit conversions
wreak havoc with templates, because inevitably what happens is
that a type is tested for whether it implicitly converts to a
particular type, but then the template is instantiated with the
original type, not the implicitly converted one, and then the
template frequently fails to compile - or if it does compile, it
may do weird things, because you're dealing with a type that
doesn't act as expected.

If you're dealing with a template which doesn't accept implicit
conversions (e.g. isNaN), and the implicit conversion were tested
after the actual type failed, and the template was then
instantiated with the implicitly converted type, then maybe that
could work, but that's not how it works now, and in general, I
think alias this is just too dangerous to use.

- Jonathan M Davis


More information about the Digitalmars-d-learn mailing list