Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:78417 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 36648 invoked from network); 28 Oct 2014 09:46:34 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 28 Oct 2014 09:46:34 -0000 Authentication-Results: pb1.pair.com header.from=are.you.winning@gmail.com; sender-id=pass Authentication-Results: pb1.pair.com smtp.mail=are.you.winning@gmail.com; spf=pass; sender-id=pass Received-SPF: pass (pb1.pair.com: domain gmail.com designates 209.85.192.48 as permitted sender) X-PHP-List-Original-Sender: are.you.winning@gmail.com X-Host-Fingerprint: 209.85.192.48 mail-qg0-f48.google.com Received: from [209.85.192.48] ([209.85.192.48:54658] helo=mail-qg0-f48.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 07/E1-21571-8F56F445 for ; Tue, 28 Oct 2014 04:46:33 -0500 Received: by mail-qg0-f48.google.com with SMTP id q108so160363qgd.21 for ; Tue, 28 Oct 2014 02:46:30 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:sender:in-reply-to:references:date:message-id:subject :from:to:cc:content-type; bh=PAYgPnibLSJY94txfe/L/+UHJaPAaFHYW5LAz0OM6v4=; b=STKPe1mt0Yj+3v8/WuWNk0mbtp6C9ghjSg40EVZwk7VXOP2LHZkfqzCfvlQDv9JO89 tNbamEmsYWt3Dc9EzsAHSKKfzJjLg4i9oUJTKMMt2NUCzOjidK6vUF6uSONJMDBTGFuS UF9mkeQozDSFpwRhJsQWO3uj1zfGO10coLruN58c9b91KQ2N0jn5eBaRQwBJZ/w3/Ni8 lumLET8lJQAS1I0is+P8S+XzuQYl0lhIa8Hm9OyjLV7qir+qIqIjijLHLHSYpbB+jcXH qr8noDrk7t6pW2aioAV4tHyXltMP4SBrZKnYLvhYyl31cksKGe0hJgd5qAbN06tsyuF6 LQgw== MIME-Version: 1.0 X-Received: by 10.140.105.37 with SMTP id b34mr2785445qgf.91.1414489590282; Tue, 28 Oct 2014 02:46:30 -0700 (PDT) Sender: are.you.winning@gmail.com Received: by 10.140.239.194 with HTTP; Tue, 28 Oct 2014 02:46:30 -0700 (PDT) In-Reply-To: <572FEBD1-99A8-4788-83F9-D99E470FB16D@ajf.me> References: <6E04B9BE-854E-4112-8C74-7D90BD8BFE95@ajf.me> <544D48A3.6070905@gmail.com> <572FEBD1-99A8-4788-83F9-D99E470FB16D@ajf.me> Date: Tue, 28 Oct 2014 09:46:30 +0000 X-Google-Sender-Auth: qUGzLRSWm3w18GdJ9PCS5d74epw Message-ID: To: Andrea Faulds Cc: Rowan Collins , "internals@lists.php.net" Content-Type: multipart/alternative; boundary=001a1137c9289142280506788514 Subject: Re: [PHP-DEV] [RFC] Readonly Properties From: cw@daverandom.com (Chris Wright) --001a1137c9289142280506788514 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable On 27 October 2014 20:27, Andrea Faulds wrote: > > > On 26 Oct 2014, at 19:16, Rowan Collins wrote= : > > > > I just had a thought on both the naming and future-proofing concerns of > this proposal: what about pre-emptively reserving the skeleton of the > syntax needed for accessors, without actually implementing them? > > I=E2=80=99ve been thinking about this. While `public readonly $foo;` does= work, it > is confusing (only readonly to public) and limiting (no public/private > option). Plus, while it could work with getters/setters, it=E2=80=99s har= dly ideal. > > On the other hand, while having C#-style syntax here gives us a full > feature set, I don=E2=80=99t like that it inverts the order of a declarat= ion. To > show you what I mean, here=E2=80=99s a normal property: > > public $foobar; > > Now, here=E2=80=99s a property with C#-style syntax: > > var $foobar { public get; private set; }; > > See how the `public` moved to the right? I don=E2=80=99t like that, I wan= t to > avoid that if possible. I=E2=80=99m all for C#-style syntax with the get = and set, > but I don=E2=80=99t like that it moves the visibility specifier. > > Similarly, I don=E2=80=99t like this either: > > public $foobar { get; private set; } > > Now the visibility is on both sides! Yuck. That=E2=80=99s the worst of bo= th worlds. > > What I=E2=80=99d like is probably something like this: > > public/private $foobar; > > Tentative syntax. But this way, the visibility stays on the left. I think > that=E2=80=99s good for readability. If you omit the second specifier, th= en the > first one applies to getting and setting, as now. If you include it, the > first one applies to getting, the second one to setting. > > It=E2=80=99d also be compatible with properties, too: > > public/private $foobar { > get { return $this->bar * $this->foo; } > set($value) { $this->bar =3D $value / $this->foo; } > } > > Sorry, but I don't like this. This means that the visibility modifier is no longer next to the thing that it applies to, IMO this is actually harmful to the readability because the order in which the accessors were defined then becomes significant so you have to either look at the ordering or have the parser enforce a particular order, plus "public/private" looks syntactically weird. I would suggest something like this: public $foobar { get { return $this->bar * $this->foo; } private set($value) { $this->bar =3D $value / $this->foo } } ...where only a single visibility modifier is permitted on the left, and this is treated as the *default* visibility for the accessors, which are free to declare another visibility if they want - so the following would be equivalent: public $foobar { public get { return $this->bar * $this->foo; } private set($value) { $this->bar =3D $value / $this->foo } } Another bonus here is that the var keyword can still be permitted on the left for those who prefer the C# syntax, as a synonym of public (i.e. the current behaviour of var). It doesn=E2=80=99t prevent truly read-only properties, either: > > public $foobar { > get { return $this->bar * $this->foo; } > } > > Does this sound like a good idea? A similar idea came up in the > discussions 8 years ago on readonly. > > -- > Andrea Faulds > http://ajf.me/ > > > > > > -- > PHP Internals - PHP Runtime Development Mailing List > To unsubscribe, visit: http://www.php.net/unsub.php > > --001a1137c9289142280506788514--