Re: What do we need strict scalar type hints for?
| From: | Christoph Becker | Date: | Mon, 02 Feb 2015 23:34:46 +0000 |
| Subject: | Re: What do we need strict scalar type hints for? | ||
| References: | 1 2 3 4 5 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-81671@lists.php.net to get a copy of this message | ||
Hi Andrea,
Andrea Faulds wrote:
> Hi Christoph,
>
>> On 2 Feb 2015, at 22:38, Christoph Becker <cmbecker69@gmx.de> wrote:
>>
>> And frankly, I don't see what's wrong with a weak type hint in this case:
>>
>> function addVat(float $amount) {...}
>>
>> You're returning a float, anyway.
>
> Given you’re dealing with currency, and given the fairly serious issues with precision loss
> with int->float conversions and vice-versa… I would be highly concerned if you used a weak type
> hint there.
Well, considering the given function body ($amount * 1.19), that doesn't
seem to be an issue. In the general case, if one has to deal with
currency, float is problematic anyway. I'd rather use a rational type
in this case (i.e. an immutable class with GMP[1] numerator and
denominator properties and the respective methods), which could be
combined with the actual currency (i.e. a "Money" class), if necessary.
>> OTOH, strict type hints would require a lot of manual conversions.
>
> Sure, they do. But they’re *explicit* manual conversions, ones you can easily grep for and
> that are obvious when reading code.
Indeed, code is read far more often than it's written (hopefully :)),
but still it might be annoying for many PHP programmers to write these
explicit manual conversions in the first place.[2]
[1] Or int, if your Big Integer Support RFC will pass (which I would
welcome, BTW).
[2] I'm aware that your current Scalar Type Hints RFC would not require
these manual conversions, but that doesn't seem to be the topic of
*this* thread. :)
--
Christoph M. Becker