Hacker News new | ask | show | jobs
by moonchild 2070 days ago
The second case is easily solved by having an overload operator+(complex lhs, int rhs). No need to convert anything.
2 comments

This fails when you want `4 + c` or `c + 4.f` etc. So you either have a gross number of overloads doing the same thing, or you just put an implicit constructor to `complex`.

Also, I agree that implicit conversions in general should be avoided, but this complex number example feels like the perfect use case.

    complex c = 4;
Would you rather have this line not compile because 4 is not a complex number? Because one _could_ argue that 4 is a complex number, and C++ can represent this with an implicit constructor.

The issue with C++ is that implicit is the default, not that it exists.

Haskell solves this reasonably well.

If your data type implements the Num typeclass, you can use literals like 4. (A typeclass in Haskell is similar to what they call an interface in Java.)

There's no automatic conversion happening at all. It's done via overloading literals at compile time. (You can do the same for strings.)

C++20 introduced concepts, and now it's possible to specify type constraints, like integral, in a way similar to Haskell typeclasses.

What it doesn't solve that by default the compiler tries to find an appropriate implicit conversion. Sometimes it's convenient, sometimes it's harder to see what the code actually does.

Yes, concepts help a bit, but I'm not sure whether they would be necessary here.

Yes, the implicit conversion for all values is part of the problem. The Haskell approach only gives you magic for literals.