Re: Scalar type hinting
| From: | Kris Craig | Date: | Tue, 28 Feb 2012 23:17:00 +0000 |
| Subject: | Re: Scalar type hinting | ||
| References: | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-58307@lists.php.net to get a copy of this message | ||
+1 on that.
However, I believe the coder should also have the option of determining
whether this should be recoverable or not, just like they have a choice
with regard to using "include" or "require" (one fails gracefully, the
other terminates execution).
That's where I think strong/weak could be very useful. Here's how I would
do it:
weak int $a = "House"; // Throws E_WARNING
strong int $a = "House"; // Throws E_RECOVERABLE_ERROR
In both cases, the error can be caught, which would mirror the flexibility
in languages like C#. However, if the weak one isn't caught, the warning
is thrown but the script proceeds as normal. If the strong one isn't
caught, the script will terminate as if it was an E_ERROR.
I think this would be the most sensible approach in terms of balancing
simplicity and flexibility.
--Kris
On Tue, Feb 28, 2012 at 3:08 PM, Michael Morris <dmgx.michael@gmail.com>wrote:
> Agreed. If conversion can occur without data loss (that is, if the
> value being assigned is == the value that actually IS assigned) then
> no error should occur.
>
> So
>
> int $a = "1"; // no error. 1 == "1" so who cares?
> int $a = 'House'; // error 0 != 'House', so this is a problem.
>
> Again, errors should only raise if the final value != source value.
>
> On Tue, Feb 28, 2012 at 6:03 PM, Rick WIdmer <vchkpw@developersdesk.com>
> wrote:
> > On 2/28/2012 2:58 PM, Kris Craig wrote:
> >
> >> strong int $a = "1"; // Converts to 1. May or may not throw an error
> (I'm
> >> still on the fence).
> >
> >
> > It this is an error, it is no longer PHP.
> >
> >
> > --
> > PHP Internals - PHP Runtime Development Mailing List
> > To unsubscribe, visit: http://www.php.net/unsub.php
> >
>
> --
> PHP Internals - PHP Runtime Development Mailing List
> To unsubscribe, visit: http://www.php.net/unsub.php
>
>