Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:109052 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 3892 invoked from network); 16 Mar 2020 11:29:18 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 16 Mar 2020 11:29:18 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 89E811804C5 for ; Mon, 16 Mar 2020 02:51:40 -0700 (PDT) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on php-smtp4.php.net X-Spam-Level: X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM,HTML_MESSAGE, LOTS_OF_MONEY,RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H2,SPF_HELO_NONE, SPF_PASS autolearn=no autolearn_force=no version=3.4.2 X-Spam-ASN: AS15169 209.85.128.0/17 X-Spam-Virus: No X-Envelope-From: Received: from mail-lj1-f172.google.com (mail-lj1-f172.google.com [209.85.208.172]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange ECDHE (P-256) server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by php-smtp4.php.net (Postfix) with ESMTPS for ; Mon, 16 Mar 2020 02:51:40 -0700 (PDT) Received: by mail-lj1-f172.google.com with SMTP id q19so17794067ljp.9 for ; Mon, 16 Mar 2020 02:51:40 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=URAwoXR8qjATIn4dlyTEJtLFN+fnY/UqEn2LdsEZ7Ck=; b=NeXm4H/YwwxOX4j74m8EIdTOt2IhiNAlGbzve/o+QiRHQd+RqtBIeX8JzLkC4I7gzc gFeU3o7+fB1hds7pJdwBMavqGR7ia07EwlFnOd7japYwxe6c1bovG39iKPZtS7PSWuOH SP/HQ3KX6foNZ+kkrvw7TWbBOWamD1q4A/hmPqNbrHmj2jfPoFcUZKewSY17IhYithjf 3x0FNRwo6NsRnEodZefNNkdL9mPEExLyPiavVZJRHP7Ruhx+jxyyeHtSCugfaPiHUyMD mKnWovLtb6t+SnJsMt4DmvNN17GEQTw86wuq63a5mV+Ng5Mcg9WfvhIq4mfeooKcfIME yjOQ== 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=URAwoXR8qjATIn4dlyTEJtLFN+fnY/UqEn2LdsEZ7Ck=; b=mvKoPM8IXQCKJFpo0tdnzF4XofOSRYdJVDgQrsDiuWfGT1ie/Xq+dNYS91mH3IXrNz 0UZOv2GL2Lgv6qLyEzwvc8E0kT0JfCYeFZpGrucrjTF4y+StLgSkbk5EgNNCxnnVzOIH QiPOWUQDuZ9Sxagxhaqunw4GSzx7msiROM7eWRfWuCLtuWjhULtKd6/ssa/lRn3cm97R RLe7lj1UgLwHrPmCNzL2nG13BwuKlSBs+MYhRpVevC/s2frjCW0L3RjAWFTlx/mYDDR+ bLH4fvk7pR3aDJMPSHh054Cy8YFDRIBKlxgcFIejRHcj8wbc2HnuKdRaZbceoCyY3iMZ GWGw== X-Gm-Message-State: ANhLgQ3e++isFsKjgyg8qG9G13X4m2fsZeZYG29ScNTJnxuVBY/7B8a+ mDhaRC3EsWUn3ALiVwGaMgyeB+qByNk3+++B+dI= X-Google-Smtp-Source: ADFU+vvHbq0lyGn2/O/tuqEziHb3GNDfs+XwKp13NzXdCfkpLYKpCg594P1u5rEdKQXVDUsAg5kzLAYukIZJ8Nlg85I= X-Received: by 2002:a2e:a0d3:: with SMTP id f19mr7246203ljm.117.1584352297489; Mon, 16 Mar 2020 02:51:37 -0700 (PDT) MIME-Version: 1.0 References: <8545d15e-ddd5-42be-8405-09697a077234@www.fastmail.com> <4d9688fe-cc57-44af-903e-05f4cbb1bbcc@www.fastmail.com> <6bcbf0a5-92d8-4cfa-a00f-e0e967fc037e@www.fastmail.com> <700327df-45d5-47ca-8828-d7ad9c9bee2e@www.fastmail.com> <6f2e7718-5d78-4c57-8da9-f8dd44cc9e7b@www.fastmail.com> <421993bf-821c-4ebf-802a-be9814b30b90@www.fastmail.com> In-Reply-To: <421993bf-821c-4ebf-802a-be9814b30b90@www.fastmail.com> Date: Mon, 16 Mar 2020 10:51:21 +0100 Message-ID: To: Larry Garfield Cc: php internals Content-Type: multipart/alternative; boundary="000000000000e418c005a0f5c3f1" Subject: Re: [PHP-DEV] [RFC] [DISCUSSION] Immutable/final/readonly properties From: nikita.ppv@gmail.com (Nikita Popov) --000000000000e418c005a0f5c3f1 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable On Mon, Mar 16, 2020 at 12:23 AM Larry Garfield wrote: > On Sun, Mar 15, 2020, at 4:44 PM, M=C3=A1t=C3=A9 Kocsis wrote: > > > > > > Avoiding that confusion will save the industry millions of dollars. > > > > > > > On the one hand, you are right, because currently it's not very useful = to > > effectively provide two > > ways of declaring a constant. On the other hand however, if we also > > consider a longer term > > aim of adding support for object default values (just like what Marco > > mentioned): > > public read-only DateTimeImmutable $startupTime =3D new > DateTimeImmutable(); > > then allowing default values for "write-once" properties seems much mor= e > > sensible. At this point, > > the "million dollar mistake" label doesn't hold anymore since class > > constants and "write-once" > > properties with default values will actually be two different things. B= ut > > we've just ended up at > > Marco's suggestion: > > I'll be honest, I really have no idea how default object values is at all > related here. I mean, those would be nice to have as well for various > reasons, but I don't see how it's related to read-only properties. > Default object values are a case where default values for readonly properties have a legitimate use-case, because the actual value will be different for each object. Currently only constant values are permissible, which will thus be the same for all objects, and as such not materially different from class constants. A few more considerations on how readonly defaults this would interact with potential future language changes... What happens if we introduce a shorthand syntax for combining property declarations, constructors and initialization? class Point { public function __construct( public readonly float $x =3D 0.0, public readonly float $y =3D 0.0, public readonly float $z =3D 0.0, ) {} } The naive transformation for this would be: class Point { public readonly float $x =3D 0.0; public readonly float $y =3D 0.0; public readonly float $z =3D 0.0; public function __construct(float $x =3D 0.0, float $y =3D 0.0, float $= z =3D 0.0) { $this->x =3D $x; $this->y =3D $y; $this->z =3D $z; } } Which of course will not work with readonly properties as defined. Alternatively one could transform this without the default values on the properties themselves, only on the arguments. That does lose out on Reflection though. The other one is the recently declined https://wiki.php.net/rfc/object-initializer. As it basically works by first creating the object normally (including a possible constructor call), and then assigning the specified properties, this would not be compatible with readonly properties that have defaults. Other implementation approaches are possible though, but may not be easily reconcilable with the need to also call the constructor. Not really sure what I'm trying to tell you with this though :) Nikita > > > 1. Prevent the parser from accepting default values on write-once > > > properties (parser error) > > > 2. Re-introduce them once we know what we want to do with default > values > > > (and it makes sense) > > > > Yes, this scenario definitely makes sense. I'm just not yet sold that i= t > > will have any negative effects > > if we don't restrict the usage of default values now. I understand that > > it's usually advantageous to be > > conservative with adding new features - especially when groping in the > dark > > - since we are the ones > > who have to support and fix them later. That's why I was hesitant to ad= d > > property covariance to the > > proposal. > > The negative effect is if we later decide that it's too much of a problem > to have default values, because they're just too confusing (people debati= ng > if they should be a constant, people expecting them to be overwriteable, > etc.), we can't simply remove them. That would be a large breaking chang= e > and not allowed, so we're stuck with 'em. > > Whereas if we don't add it now, we can see if people are really clammorin= g > for it and in what situations. We let the PHP-using masses do the resear= ch > for us to determine how read-only-default would actually be useful, or if > it would be useful at all. Then we can add it later in a way that would > actually be useful, or decide not to do it at all. > > --Larry Garfield > > -- > PHP Internals - PHP Runtime Development Mailing List > To unsubscribe, visit: http://www.php.net/unsub.php > > --000000000000e418c005a0f5c3f1--