Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:103181 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 31344 invoked from network); 19 Sep 2018 19:50:41 -0000 Received: from unknown (HELO mail-lj1-f193.google.com) (209.85.208.193) by pb1.pair.com with SMTP; 19 Sep 2018 19:50:41 -0000 Received: by mail-lj1-f193.google.com with SMTP id p6-v6so5551921ljc.5 for ; Wed, 19 Sep 2018 08:57:05 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=ajNLWKDUjmb7NDZKUiz5kaRbZHJxvoKJ0JSTV7brP+U=; b=cq0vGgT+IdEiGVWPeEKJTjj1eSLsWaa2IPqq11aTal7RCm5KQacB2gf493WkgwIE0T HcGWerS18+e02vN6wMvuyRlaySuxll3zghi+O6QcS5O66TRSCAq3+x2dRdxNip61/9O6 M5WStMXvF49j2tKYFKW6+7XBthBeDJrFttLssQQH0AGtui4w48OhomwF/yljGHNlz3+w 2ONX0QzMIor4MaZMcuJLL0n3AsgYkSS4ikODqkmbJX32ajpn4ZTz/maiKeZMpZNUZeUp 5bFgG6uRmF4/q+WKhNVuXEjeLjxg6LQjGmEcwo+9Lz7qvVmt3Xx6+8xJS1zZHSMuxvpx tAmQ== X-Gm-Message-State: APzg51BmA/omi/AiE7mK66xj2LD7lh5lPzsPh2tvVeiTi0EeHSkc2uz7 KCzHKVYHf/eVgBoy6vMM+Y+Et5Mfbv2NRoMAYQk= X-Google-Smtp-Source: ANB0VdYufK/xiPa7MqHWUvktO4RRJer7jnIp5zyqHd4/bhsUR2iAmYZ+YgQrEyFB49Q6NOY+kI4K0odg8uuylChgUcU= X-Received: by 2002:a2e:2f19:: with SMTP id v25-v6mr5566085ljv.113.1537372624500; Wed, 19 Sep 2018 08:57:04 -0700 (PDT) MIME-Version: 1.0 References: In-Reply-To: Date: Wed, 19 Sep 2018 09:56:48 -0600 Message-ID: To: Rowan Collins Cc: internals Content-Type: text/plain; charset="UTF-8" Subject: Re: [PHP-DEV] [RFC] [VOTE] Typed properties v2 From: levim@php.net (Levi Morrison) On Wed, Sep 19, 2018 at 6:38 AM Rowan Collins wrote: > > On Tue, 11 Sep 2018 at 08:05, Bob Weinand wrote: > > > Hey, > > > > As announced, we are starting the vote on typed properties today. > > > > The voting period is two weeks, until sometime in the evening on Tuesday > > 25-09-2018. > > > > Please find the RFC at https://wiki.php.net/rfc/typed_properties_v2. > > > > > For the record, I still think we will come to regret allowing non-nullable > type hints without any constraint on the author of the class to initialise > them correctly. As proposed, the invalid state may only be detected when it > causes a runtime error in completely unrelated code. > > I gather that the authors of C# are currently going through a painful > development phase to introduce better support for non-nullable types, and > having to include many compromises and handle many edge-cases because it > was not done earlier. I realise this case is not completely comparable, but > we have an opportunity to get this right first time, and not just take the > easy option. > > I am therefore going to make one last plea: if we don't yet know how to > assert that complex types are initialised, do not allow them to be > non-nullable in the first version of this feature. > > That is, allow `class Foo { public ?Foo $foo = null; }`, but not `class Foo > { public Foo $foo; }`. > > This would still be a huge improvement to the language, but leaves us free > to design additional features to prevent Unitialized Property Errors > becoming as hated as Null Pointer Exceptions in Java or C#. > > Regards, > -- > Rowan Collins > [IMSoP] I posit that this code: class Foo { public Foo $foo; } Is superior to this code: class Foo { public ?Foo $foo = null; } If after "initialization" that `$foo` is guaranteed to always contain an object of type `Foo`. The reason is simple: for the actual lifetime of the object the former correctly states that it will never be null, while the latter opens up possibilities of it. That is, the former provides *better* support for non-nullable types. To prevent all forms of initialization errors we would have to do analysis at the initialization site and prevent dynamic behaviors in that region. For now I believe such things are better left to static analysis tools than the engine.