On 4/14/16, 12:35 PM, "Levi Morrison" <morrison.l...@gmail.com on behalf of le...@php.net> wrote:
>I can appreciate that you want only the restricted union with null. >However, I do not see the point of disallowing it for parameter types >while allowing it for return types: > > function setLeft(Node $n = null) { > $this->left = $n; > $this->updateHeight(); > } > >Why disallow the explicit union with null here instead of the default >parameter which does not exactly capture the desired semantics? >Calling `$node->setLeft()` is just odd, almost as if it was a mistake. >I would much prefer `$node->setLeft(null)` here. Basically, if we have >a feature for return types that exactly matches the semantics that we >occasionally want for the parameter types why forbid it? I agree that `$node->setLeft()` is weird but I find `$node->setLeft(null)` still a bit weird. Perhaps something like `$node->resetLeft()` would work? Was that the idea? >Additionally, on occasion I'll see functions like this: > > function foo(Bar $b = null, $not_optional_param); The only thing we can know for sure from this is that the programmer urgently needs reeducation :) >Why not allow nullable types on parameters to avoid that wonkiness >caused by default values of null? > > function foo(Bar | Null $b, $not_optional_param); > >This is much better. Yes but still a code smell to me. I'd need to know more about the programmer's intent for `foo(null, "value")`. It might be better to swap order, or change the method name, or add another method... Who knows? Need to take each case individually. This kind of asking questions about intent in code review is good for code quality. That's why I like how PHP doesn't allow this. It encourages the question asking. Every case is different, of course, so you can surely find counter examples. But on balance I'd say it's better to disallow it. Does this help you understand my preference? I think the restriction encourages a healthy discipline. Otoh, I think nullable return is a pressing need. Tom -- PHP Internals - PHP Runtime Development Mailing List To unsubscribe, visit: http://www.php.net/unsub.php