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.

    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.

-- 
Richard "Fleshgrinder" Fussenegger

Attachment: signature.asc
Description: OpenPGP digital signature

Reply via email to