What did you think about an implicitConversionOp ?

H. S. Teoh hsteoh at quickfur.ath.cx
Sat Dec 26 19:07:00 UTC 2020


On Sat, Dec 26, 2020 at 06:42:43PM +0000, sighoya via Digitalmars-d wrote:
> On Saturday, 26 December 2020 at 18:12:02 UTC, H. S. Teoh wrote:
> > Implicit conversions are admittedly very convenient in the short
> > term, but in the long term they tend to make code obtuse and hard to
> > maintain.
> 
> Could you elaborate a bit more?
> 
> Did you have to many times pain with ambiguity errors or did your
> program run unexpectedly by resolving to the wrong method?

It wasn't compile errors necessarily, but it was difficulty
understanding what exactly the code is trying to do.  And also painful
when you thought one function was returning one type, but it actually
returns a different type, so you can't pass it to the next function, but
have to insert an extraneous conversion back to the original type. Or
worse, you need to store a value in a struct somewhere, but due to
implicit conversion the type you get out of a function does not match
the type in the struct, and changing the struct's field type will lead
to a huge amount of changes.  There have also been occasions of actual
bugs introduced by implicit conversions -- because some value implicitly
converted earlier than expected, or got to the end via a different chain
of conversions than you expected -- though this is relatively rare.

Of course, you can always work around all of this somehow, perhaps by
adding even more implicit conversions.  Eventually, the code just
accumulates all sorts of weird hacks and you're spending more time
working around implicit conversions (or trying to understand them!) than
actually making progress in the problem domain.  It becomes a constant
source of frustration and waste of time.

Writing code without implicit conversions is tedious and annoying in the
short term, but in the long term actually saves time by making the code
clearer and more explicit, and more maintainable.


T

-- 
"I'm not childish; I'm just in touch with the child within!" - RL


More information about the Digitalmars-d mailing list