Of course combining string + false + null makes sense, we use that every day 
with core functions and libraries,
but combining string with bool (true and false), int, float, etc makes no sense 
to me.

let's look at an example from some older mdb2 code:

/**
 * Tell whether a value is a MDB2 error.
 *
 * @param   mixed   the value to test
 * @param   int     if is an error object, return true
 *                        only if $code is a string and
 *                        $db->getMessage() == $code or
 *                        $code is an integer and $db->getCode() == $code
 *
 * @return  bool    true if parameter is an error
 *
 * @access  public
 */
function isError($data, $code = null)
{
  if (is_a($data, 'MDB2_Error')) {
    if (is_null($code)) {
      return true;
    } elseif (is_string($code)) {
      return $data->getMessage() === $code;
    } else {
      $code = (array)$code;
      return in_array($data->getCode(), $code);
    }
  }
  return false;
}

Here $code would be "array | string | int | null", but to understand the 
function, we still need to read the code or the phpdoc.

But we could do:
function isError($data, array | int $matchCodes = null, string $matchMessage = 
null): bool

Then it would be clear that we can't match an array of messages.
But as said before string | int | ... makes no sense to me.

Regards
Thomas


Jordi Boggiano wrote on 03.06.2016 17:16:

> On 03/06/2016 15:58, Thomas Bley wrote:
>> To me type declarations help to make my code easier and more consistent.
>> Having multiple scalar types for a single function parameter is against this
>> goal since I need extra logic to handle this.
>>
>> e.g. function foo(string | int | bool $bar) {} makes no sense in weak mode
>> since string can already handle int, bool, float, etc.
>>
>> having different behavior between
>> foo("42"); function foo(string $b) {echo gettype($b);} // string
>> and
>> foo("42"); function foo(string | int $b) {echo gettype($b);} // integer
>> also makes no sense to me.
>>
>> Things like string|array are useful (e.g. str_replace) since we can cast the
>> string easily to array and calling a string parameter with an array would 
>> give
>> a fatal error.
> 
> That is a useful case, and don't forget also return values, e.g. all the 
> XX|false-kind of return types it's also nice to have.
> 
> I don't think for function arguments it's massively useful and I doubt 
> it'll get put everywhere, but it's nice to be able to express this when 
> you have to.
> 
> Cheers
> 
> -- 
> Jordi Boggiano
> @seldaek - http://seld.be
> 
> -- 
> 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

Reply via email to