[Issue 20433] Don't complain about implicit cast when an explicit cast would also be in error

d-bugmail at puremagic.com d-bugmail at puremagic.com
Tue Dec 17 14:12:38 UTC 2019


https://issues.dlang.org/show_bug.cgi?id=20433

RazvanN <razvan.nitu1305 at gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |razvan.nitu1305 at gmail.com

--- Comment #1 from RazvanN <razvan.nitu1305 at gmail.com> ---
I think that the error messages are fine. When someone sees "cannot implicitly
convert expression [1] of type int[] to Thing", I would argue that the first
think that comes to mind is "Holy Crap, I tried to put an array of ints into a
Thing type, the correct thing to do is Thing a = Thing(1);". Let's just assume
that my supposition is false and the majority of people think "oh, it wants me
to explicitly cast the int array to a Thing" (?!?!?!), then if you explicitly
cast it you get an error message that it is impossible. Now you are left with
no other option than to try instantiating a Thing with a Thing (which is the
correct behavior).

As for the situation with both implicit conversions and casts: if you have a
situation where both fail, the compiler will highlight one at a time, so your
scenario is not possible. Example:

struct Thing { int x; }
struct A {}
void main() {
    A a = cast(Thing) [1];
}

Error: cannot cast expression [1] of type int[] to Thing

This behavior is fine and I suggest we close as this INVALID.

--


More information about the Digitalmars-d-bugs mailing list