Re: Type hinting misunderstood
| From: | Sam Barrow | Date: | Sun, 06 Jan 2008 15:57:27 +0000 |
| Subject: | Re: Type hinting misunderstood | ||
| References: | 1 2 3 4 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-34531@lists.php.net to get a copy of this message | ||
On Sun, 2008-01-06 at 15:59 +0100, Magnus Määttä wrote:
> On Sunday 06 January 2008, Alain Williams wrote:
> > On Sun, Jan 06, 2008 at 01:02:54PM +0100, Stefan Esser wrote:
> > > Hello Alain,
> > >
> > > I think you are also confused about PHP type hinting...
> > >
> > > The manual clearly states:
> > >
> > >
> > > > Type Hinting
> > >
> > > > PHP 5 introduces Type Hinting. Functions are now able to force
> > > parameters to be objects (by specifying the name of the class in the
> > > function prototype) or arrays (since PHP 5.1).
> > >
> > > And suddenly you want to change that definition to
> > >
> > > "Functions are now able to force parameters to be types that *could be
> > > casted* to objects/arrays."
> > >
> > > That is not going to happen. Type hints are supposed to force parameter
> > > to be of a specific type. Any kind of auto conversion would redefine
> > > type hinting and would be just another feature that gives PHP haters a
> > > reason to rant about.
> > > And if you want that '1' is allowed to be passed as parameter (without
> > > an actual int cast) because it COULD BE CASTED to int then you gain
> > > ABSOLUTELY nothing from the type hint. Then you don't need to introduce
> > > them at all.
> >
> > No - you have misunderstood me. The actual cast IS part of the proposal..
> >
> > My mail showing how the checking works might have not be clear on the cast
> > part - I was talking about the checking -- which has to happen before a cast.
> > The point that I was trying to make is that the VALUE is preserved. Within
> > the function type juggling may still happen, allowing an int to be used
> > as a string (or whatever).
>
> This would only be confusing and not anything like the original proposal, or
> anything that I would support or have any use of.
>
> Type hinting should force the type, not cast it to the type specified. This
> proposal is about forcing types and I'm sure pretty much everyone would
> expect a type-hinted param to be of a forced type, not converted, like every
> other language I've ever used.
This was the intention I had when I wrote the patch. Automatic type
casting would go against the rules of array/class type hints, and
require a completely new patch.