Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:103186 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 61384 invoked from network); 19 Sep 2018 22:59:48 -0000 Received: from unknown (HELO mail-pf1-f172.google.com) (209.85.210.172) by pb1.pair.com with SMTP; 19 Sep 2018 22:59:48 -0000 Received: by mail-pf1-f172.google.com with SMTP id k21-v6so3129661pff.11 for ; Wed, 19 Sep 2018 12:06:14 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=mindplay-dk.20150623.gappssmtp.com; s=20150623; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=+v29ly2Oc5vxbTnc58xgqTGDnWuUZqh/SDlsn1TuNJc=; b=XTkai22F7jvfIsW0chgnORRLJrguVeTQqS1YmGKpdw9+1NsXtRvSOoBrrJmcflMeGV LfJTmV04Vy2njn04IXHcmvvOUvMGdyHH6rba5mhQJRmSivVIi0NObhgVFfGPJMR7HOX6 FJ7bTkObMHRYJEHi4TDo/VLisAgIor31lv731VBePr+o8FqUKYaxUFGMb4QV2MrNKUpz fqGJeLzyaq680dVauSsJS1mbi32xsVr71MBWnoeSVWB8HQndw1uAp/ubcf88Ip6yqjzU 7ji6qrfaqp9x2l60LI1jKVanUOtERnMOG76aUDsRpq7CeSq1JY5zzOAvQ5DZpZ0q6zKo HyPw== 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=+v29ly2Oc5vxbTnc58xgqTGDnWuUZqh/SDlsn1TuNJc=; b=CKGOmrZJspiMSFr6+0Ek9WbzGePM2gcQHIYy1P3jRTIIsBfW5+4ILMPoomHIDyish2 zDjgO30LZsVgVeqiD7v07Y8Ozystk/RsaBrcit+jp5AI3x944GXMJeGT81mAmIOPNypV lrMgO4mUspuPqkl1H5p668g/kelmAC8nV/9SGO5ieJyNLqvE81LbIGyuaiZpZ7nMDUhB KcUUXW3KMg0ixoTc768NXx98YR18yDrfQz/q92Y7ZoDH2N0YaRIA29J/01KwzOmoL9fl FB/SPPHwqfqjDftAdtOCPagV7iXVzMPuAR1KlIDg4PY07MhF4UPE3/9UxxutlZEEr8/L onzw== X-Gm-Message-State: APzg51DqvCBCyxIS5CeHguIFDjLgMsWBomqkm/LcAMWOKyfJ7jYEsGIE Xgd16jt2JBkbzNiPOzrmG8JtNTu+c6GZDWLLD0AEkA== X-Google-Smtp-Source: ANB0VdYa+BgF6kfW0vXO9iMauljbFi65PY0RXLotyUyTOJ6Z6qy+3xFYvc4o4sxuyJpIWrXwAqzZ4Q2uhrqhR9KCpIs= X-Received: by 2002:a63:1064:: with SMTP id 36-v6mr33579889pgq.254.1537383973675; Wed, 19 Sep 2018 12:06:13 -0700 (PDT) MIME-Version: 1.0 References: In-Reply-To: Date: Wed, 19 Sep 2018 21:06:03 +0200 Message-ID: To: rowan.collins@gmail.com Cc: PHP internals Content-Type: text/plain; charset="UTF-8" Subject: Re: [PHP-DEV] [RFC] [VOTE] Typed properties v2 From: rasmus@mindplay.dk (Rasmus Schultz) On Wed, Sep 19, 2018 at 7:43 PM Rowan Collins wrote: > I agree that this is a hard problem, but I don't agree that this decision > is being made "for now". If we allow "non-nullable but uninitialized" > properties now, it will be extremely hard to change their behaviour in > future. I'm with Rowan on this one. This concept of "uninitialized" frankly seems like an allowance for people who insist on writing poor code. Nulls are bad, and "unintialized" is just another kind of "null" with a built-in run-time type-check that executes on read - too late. The first example given is just bad: class Point { public float $x, $y; private function __construct() {} public static function fromEuclidean(float $x, float $y) { $point = new Point; $point->x = $x; $point->y = $y; return $point; } } You define two invariants: $x and $y must be floats - and then proceed to break those constraints in the constructor? Wrong. The RFC itself accurately states that "this code can be rewritten to indirect through __construct() instead" - as shown in the previous example. Now why would you deliberately open the fences and knowingly invite people to write poor code like this? As for the second example: class Point { public float $x, $y; public function __construct(float $x, float $y) { $this->doSomething(); $this->x = $x; $this->y = $y; } } If doSomething() attempts to read an uninitialized property while the constructor is still executing, throwing a helpful "uninitialized" error is fine. But, in my opinion, once the constructor has executed, the invariants as declared by the class itself must be satisfied. If there's one meaningful use-case for allowing objects in a partially-initialized state, it's during hydration/unserialization/reflection scenarios, maybe - but in those cases, you're willfully bypassing the constructor; it's not the everyday 95% use-case and some risk is acceptable here, you'll get around it with tests. But nobody wants to write tests all day to see if any classes contain "unininitialized" properties - that misses half the whole point of being able to declare those types in the first place, e.g. makes type-hinted private/protected properties totally unreliable. Once this is in a release, it'll be unfixable, and in my opinion will likely go down in history as another one of those little things we wish we could go back in time and fix :-/