Re: [PHP-DEV] Warning for "confusable" types

This is only part of a thread. view whole thread
  107533
October 14, 2019 07:21 php@beccati.com (Matteo Beccati)
Hi Stas,
> Maybe we should just change the error message to this?> > must be an instance of the class "resource", but resource type is given> > OTOH, since there are actually no resource type hints, and
naming your> class "resource" is an extremely bad idea, having a warning there> wouldn't hurt too. I was about to give my +1 on the more generic approach, but type hints could also be interfaces and "instance of the class X" is not a good fit. Cheers -- Matteo Beccati Development & Consulting - http://www.beccati.com/
  107536
October 14, 2019 07:53 smalyshev@gmail.com (Stanislav Malyshev)
Hi!

> I was about to give my +1 on the more generic approach, but type hints > could also be interfaces and "instance of the class X" is not a good fit.
We could use "class or interface" if that's important. -- Stas Malyshev smalyshev@gmail.com
  107539
October 14, 2019 08:17 gertp93@gmail.com (Gert)
On that note, since traits aren't possible as a return type. Would it
be possible to determine that the given return type is a trait? In
that case a more specific error message could also be possible.
I don't know much of the internals, so i don't know if that is even
possible atm, but it would be a nice addition.

On Mon, 14 Oct 2019 at 09:53, Stanislav Malyshev <smalyshev@gmail.com> wrote:
> > Hi! > > > I was about to give my +1 on the more generic approach, but type hints > > could also be interfaces and "instance of the class X" is not a good fit. > > We could use "class or interface" if that's important. > > -- > Stas Malyshev > smalyshev@gmail.com > > -- > PHP Internals - PHP Runtime Development Mailing List > To unsubscribe, visit: http://www.php.net/unsub.php >
  107548
October 14, 2019 14:49 nikita.ppv@gmail.com (Nikita Popov)
On Mon, Oct 14, 2019 at 9:21 AM Matteo Beccati <php@beccati.com> wrote:

> Hi Stas, > > Maybe we should just change the error message to this?> > must be an > instance of the class "resource", but resource type is > given> > OTOH, since there are actually no resource type hints, and > naming your> class "resource" is an extremely bad idea, having a warning > there> wouldn't hurt too. > I was about to give my +1 on the more generic approach, but type hints > could also be interfaces and "instance of the class X" is not a good fit. >
We've tried to improve these error messages a couple of times already, which is why it now say something like "an instance of resource, resource given" rather than just "resource, resource given". This does help, but there's a couple of disadvantages of trying to clarify the error message when a type error occurs, rather than throwing a warning during compilation: * The error is introduced during the function declaration, because that's where the incorrect type is used. Later errors will generally point to uses of the function, which is not where the error lies in this case. * There are many different errors that can be triggered. There's argument types, return types, property types, reference types, as well as things like default values (something I saw recently: Someone being confused about why "public double $foo = 1.5" throws an error.) All of these use different error messages and it's something of a loosing battle to try and address this issue in all of them. * Changing type errors is extremely costly, both as a one-time cost (test updates) and in terms of maintenance. The current type errors we throw for function arguments and returns are quite specialized, which makes it unnecessarily hard to incorporate future type-system extensions. For property types we went down a much simpler route of just printing the canonical type name and not specializing anything, which would get ugly pretty quickly, especially for reference errors that involve more than one type. This is why I think that this confusion needs to be addressed at the root, when the type is actually used, not at some later point. Nikita