On 04/07/2016 08:55 PM, Fleshgrinder wrote:
On 4/7/2016 3:55 PM, Dmitry Stogov wrote:
On 04/07/2016 03:44 PM, Bob Weinand wrote:
Am 6.4.2016 um 10:45 schrieb Dmitry Stogov <dmi...@zend.com
<mailto:dmi...@zend.com>>:
1) While parameters allow null to be accepted as the default value,
null is never a valid value for a typed property.
I think we must have a way to make properties explicitly nullable.
Otherwise we won't be able to define even simple binary tree...
class Node {
public Node $left = null;
public Node $right = null;
}
Eventually that, but I honestly would prefer to be more explicit with
a typed union
public Node | null $left;
This is not a subject of this RFC. This is about "union types" vs
"nullable types".
Actually this is not true because an union type with null is the same as
a nullable type.
class Node {
public Node? $left;
public Node|null $right;
}
Both definitions are equivalent and allow the compiler to reason and
ensure type safety. Using the null assignment as you propose in your
example is not possible as Andrea Faulds pointed out to me and where she
is completely right. Simply because it makes it impossible for you to
have another default value.
Today, PHP works with arguments in this way.
If we decide to enable nullable "?" syntax and/or "union types" we will
have to use them for properties as well.
but for now, we don't have anything accepted yet, and we are going to
push both RFC on next week.
class StupidExample {
private int? $x = 42;
public function setX(int? $x = 42) {
$this->x = $x;
}
}
$o = new StupidExample();
$o->setX(null);
Note how the nullable (union null) type is much more useful than the
current implementation in PHP.
I don't see a lot of use cases, but it's not a problem to implement this.
Actually the implementation was proposed more than a year ago.
https://wiki.php.net/rfc/nullable_types
Thanks. Dmitry.
--
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php