Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:91711 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 35455 invoked from network); 16 Mar 2016 19:30:48 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 16 Mar 2016 19:30:48 -0000 Authentication-Results: pb1.pair.com header.from=narf@devilix.net; sender-id=pass Authentication-Results: pb1.pair.com smtp.mail=narf@devilix.net; spf=pass; sender-id=pass Received-SPF: pass (pb1.pair.com: domain devilix.net designates 209.85.214.174 as permitted sender) X-PHP-List-Original-Sender: narf@devilix.net X-Host-Fingerprint: 209.85.214.174 mail-ob0-f174.google.com Received: from [209.85.214.174] ([209.85.214.174:32971] helo=mail-ob0-f174.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 02/6D-48430-664B9E65 for ; Wed, 16 Mar 2016 14:30:48 -0500 Received: by mail-ob0-f174.google.com with SMTP id fz5so61810877obc.0 for ; Wed, 16 Mar 2016 12:30:46 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=devilix.net; s=google; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :cc; bh=W1W6c2W3huiOmr3twPtNoGSuGWc74fUdFWms6crDv2c=; b=lLqwuZLFkY0a5nEchrzeFNpYqOalYOAzochXrpo+YfwBGr7bQF8q8ei2VotJPzZ8zg EKehZm8HqQDAQ3kwXJ0yZWQcoft50l50JJXJJjxfcap35gMb9Kk+e5U7rknWGQ83Opkg fSyThmPisiSaaHfZluuYw4SkLOEuYYFBN7DMA= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:mime-version:in-reply-to:references:date :message-id:subject:from:to:cc; bh=W1W6c2W3huiOmr3twPtNoGSuGWc74fUdFWms6crDv2c=; b=XqlL+2UKgA30hcT2AUXRBR2P8D1OEEZ1WMefUQdmt9RDewK7lRxJiyfOn5LsPRLvCE XPYIXD3dUTJAyE4yIawzYG0MjHMSYkWPSayPswhPOLh9x4gRdSPT81mFfknaMD5EiOrC Miq8eSneEbfpyd4L7RtJAcrdBrLDzGYNu+EP7DgaE7plxYF7iFqihaAPBU7c2hm4gzvj 4rn9U6e6k5T/ERJLtnHaAIOktmk6SygP8+tVEipTvAn+V7fk9Y+3PLsyPjPaKElVK2db hINYGsQzlHAEISZnx2FtuCBUonyXBc4jY41Qa0PEsSJMPAkV6kRMln93uyg5DJrgCZsa ixpA== X-Gm-Message-State: AD7BkJLr9o+52XjyGHcPnlCLRE3pK/SRPUT5/t7K10Fjp3vucrTlEJ+Kef/TZXPI2vT4DsiH6zSuAe5DBRDUXw== MIME-Version: 1.0 X-Received: by 10.182.225.231 with SMTP id rn7mr3837476obc.2.1458156644242; Wed, 16 Mar 2016 12:30:44 -0700 (PDT) Received: by 10.202.175.74 with HTTP; Wed, 16 Mar 2016 12:30:44 -0700 (PDT) In-Reply-To: References: <56E9AAF1.7070200@garfieldtech.com> Date: Wed, 16 Mar 2016 21:30:44 +0200 Message-ID: To: Ryan Pallas Cc: Larry Garfield , "internals@lists.php.net" Content-Type: multipart/alternative; boundary=001a11c2f202ceaf3c052e2f8ca0 Subject: Re: [PHP-DEV] [RFC Discussion] Typed Properties From: narf@devilix.net (Andrey Andreev) --001a11c2f202ceaf3c052e2f8ca0 Content-Type: text/plain; charset=UTF-8 Hi, On Wed, Mar 16, 2016 at 9:21 PM, Ryan Pallas wrote: > On Wed, Mar 16, 2016 at 12:50 PM, Larry Garfield > wrote: > > > On 3/16/16 11:36 AM, Phil Sturgeon wrote: > > > >> > >> 2. This whole temporary nullability situation, where unset properties > >> will error on attempted usage if not set. Should they instead error > >> after the constructor has been called if they are still not holding a > >> value? > >> > > > > I fall back to a statement I made in a blog post a while back: > > > > http://www.garfieldtech.com/blog/empty-return-values > > > > " But consider what you could do instead that would not force me to throw > > is_null() around my code, because throwing is_null() around my code makes > > me sad. NULL should be the return value of last resort, because it means > > nothing. (Literally.)" > > > > Allowing default-null on properties means that as someone using that > > property, I have two options: > > > > 1) Throw a lot of is_null() calls around my code. > > 2) Assume that whoever initialized the code provided a value by the time > > initialization is done and skip those extra checks. > > > > Insert that old adage about what happens when you assume. > > > > End-of-constructor checks seem like a good approach; they have to be > > uninitialized at some point when new is called initially, but they should > > be guaranteed set as soon as possible. End of the constructor is "as > soon > > as possible", and I think reasonably static-analysis-catchable. (Meaning > > my IDE can yell at me appropriately.) > > > > That removes is_null() calls from the rest of my codebase, which is a > good > > thing. > > > > > I'm not sure I like the "after contructor" because that's not necessarily > when the object is finished being initialized. Consider this: > > abstract class Contract > { > private function __construct() {} > > public static function fromArray(array $aProps) : Contract > { > $s = new static(); > foreach ($aProps as $k => $v) { > if (property_exists(static::class, $k)) { > $s->$k = $v; > } > } > > return $s; > } > } > > class User extends Contract { > public int $id; > public string $name; > } > > $user = User::fromArray(['id' => 123, 'name' => 'derokorian']); > > In this example, the constructor finishes but the properties still are not > initialized at time of finishing. This is a pretty common pattern as well. > Also, a LOT of code would end up with e.g. a default value of 0 for an integer property, still having to check if it was properly populated with a positive value - not much of an improvement over NULLs. Cheers, Andrey. --001a11c2f202ceaf3c052e2f8ca0--