Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:115448 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 2352 invoked from network); 17 Jul 2021 06:14:47 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 17 Jul 2021 06:14:47 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 2A67D1804C8 for ; Fri, 16 Jul 2021 23:39:13 -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, RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H2,SPF_HELO_NONE,SPF_PASS autolearn=no autolearn_force=no version=3.4.2 X-Spam-Virus: No X-Envelope-From: Received: from mail-yb1-f171.google.com (mail-yb1-f171.google.com [209.85.219.171]) (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 ; Fri, 16 Jul 2021 23:39:12 -0700 (PDT) Received: by mail-yb1-f171.google.com with SMTP id x192so18512808ybe.6 for ; Fri, 16 Jul 2021 23:39:12 -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=L2Ns2ovHgJtkETVnaDU19mIChOxRM63UgjJ0VBMFGo4=; b=va1UnR4O8ukUEc+w7i3D2LeaQIND6D4MxFXisPVo5vcNhEDFe6+bjhbL098NzzelWY jPtSdS+eRIjfd4M7NjLkFbeDN1OEeeZr5Qt4EqklDhb05LJooOgDz8V4i1m7LSXIP77b NyIndjPKt/JDRHhQ8Z9UTSRsuXrB4TnGnmEF1rCA3br54dfp4t0lYk7295jura2+tZKr 6M0NfNGjX1KxmVMjDtq09Um4ZTuOtj/P7GY07di9HEbWzScaa0DqUGIRovuzmHqJjgUl O1x0bf2CxxL6pBxuUE4KN9hgNyHNIEIEvmndjyEdHVAt9cWyHxBjnSMbt/P8x4VN1vUd fIyA== 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=L2Ns2ovHgJtkETVnaDU19mIChOxRM63UgjJ0VBMFGo4=; b=bB0MyN8Y1TtFz1lo/SSNtJsK+TLH5yXL6LkUFajE2EUL2ux5sJ66fMuPD/vgoPcnSq JyAw7ssd0m5JnsEzd0W9k1Fo3TmI7mi0DJJAMWI9kG8WDvWL9oO+Q6WasPOoDDv31vMc JOb4792RdDkPV8M7v0WgLR19FWteXLQUS9Q82yIFy085sZdc7gDb8oR+6UpWLaLs5Ebg ZeDIAo9Sl9CBiGumKliMoR702EtvHbXcTigOo6u93PhnDBSCsP/r6P6gVcFGLQ1RvhJm c8jvKSSzhB8ACxp1dIVW045lprY2V899IE8R9rkYxeJarUFYaUxGmcUaL7WMtgZB63wz 7RoQ== X-Gm-Message-State: AOAM53095dnsq6kdoij74dQ3QRMlotD4ZlwzoDQ1sv1j2Qb9fWWx77RT 3bAnSQDs4RdvPJWdkhmAoUiPv9ezn23W7tz+3CrHuxhg5ErzdA== X-Google-Smtp-Source: ABdhPJyKj/BT7dhuvRNmPQBH/MhonRJgInJstCrDzi8kH9DaNhx9DY4YRac5QLUlbXJtZz7O3ZZSpYPYPF1e44rw1G4= X-Received: by 2002:a25:71c4:: with SMTP id m187mr17526606ybc.397.1626503951860; Fri, 16 Jul 2021 23:39:11 -0700 (PDT) MIME-Version: 1.0 References: In-Reply-To: Date: Sat, 17 Jul 2021 09:39:00 +0300 Message-ID: To: Larry Garfield Cc: php internals Content-Type: multipart/alternative; boundary="00000000000046c5dc05c74bf64f" Subject: Re: [PHP-DEV] Readonly properties - immutability by default From: zsidelnik@gmail.com (Eugene Sidelnyk) --00000000000046c5dc05c74bf64f Content-Type: text/plain; charset="UTF-8" > Please don't top post. This is a bottom-post-centric list. Can you please tell me what mailing client you use and what should I? > if we were designing the language today we would do it very differently. This reminds me working with legacy code in the team which says to write the code in the style in which the project is written. You are trying to make your code more maintainable and readable, decompose your functionality, define clear pre and post conditions. Then comes time for code review and teamlead orders you to throw everything away and write the same legacy as they do because project is written this way. PS. No more do I work in this team. Can you please explain what prevents us from doing the best (like if we were designing the language today) for new features now? 1) Dollars in PHP > Variables in PHP always begin with $. For all time, Always. Yes, but let's not confuse variables (which by definition can be rewritten) with values that can get it's value only once. Almost every time we encounter $ sign in PHP, we can change it's value either as a $property or $variable. All the time we encounter "immutable descriptor for value" (fancy name for not-variable) without dollar sign, it can't be changed. I am talking about constants. 2) Object constants > Objects do not have constants. Classes have constants. Yes, objects do not have constants, but may have `readonly` properties which use constant-like syntax because they can't be ever changed. I don't understand why you name it constants if it is not. > Introducing object-level constants is potentially confusing. Yes, introducing constants may be confusing. Introduction of `readonly` properties with dollarless syntax is very logical. 3) Constants and Properties > Constants shouldn't be set even once. Properties can be, but properties have $ on them. Again, dollar sign is used for mutable value descriptors. Constants don't have dollar sign and are not overwritable. Readonly properties should not have dollar sign because they are not overwritable as well. 4) Reflection part doesn't seem to be complicated here > Are they still properties according to reflection, or are they something else? According to reflection, these are properties. > What would reflection do with these object constants? The reflection part should remain the same as it is with current RFC so that `ReflectionProperty::isReadOnly` is added as well as `IS_READONLY` flag added to the modifiers list. > Can I enumerate mutable properties and constant properties together or separately? Why would you need to enumerate them separately? All of them are properties by definition. > Having very subtly different syntax for a very significant behavioral difference in properties seems like a landmine waiting to happen that would only confuse people. We are in the programming world. In PHP if someone gets undermined, he will know what he did wrong right in a few seconds. After blowing up (if this will ever happen), programmer will write the code with understanding how and why it works this way. On Fri, Jul 16, 2021 at 7:14 PM Larry Garfield wrote: > On Fri, Jul 16, 2021, at 6:48 AM, Eugene Sidelnyk wrote: > > @Nikita Popov I'm not sure what you mean by > saying > > this: > > > > > We're always explicit at the declaration site, it's const FOO, function > > foo, class Foo etc > > > > > > Regarding your message > > > > > Here mutability is decided by single $ character in the declaration, > > which doesn't have a particular obvious connection to mutability. > > > > Yes, that is not really obvious about property mutability when we look at > > the code. > > I think it is probably better to rephrase it into something like: New way > > of properties definition without dollar sign. Not only do we remove > dollar > > sign, but these properties can not be reassigned. > > > > Looking from this perspective we are trying to improve code quality and > > developer experience. Using immutability (as well as type-hints with > strict > > typing) makes code less error prone. This should be promoted in the PHP > > community. > > What I want to say is: good things should be easily done, easier than > > things which are more error prone. > > > > Again, about explicitness and obviousness. > > Imagine (just for a second) that all existing properties are immutable by > > default. Then someone submits RFC to add mutability for them. It is > > accepted to add a new keyword like this: > > > > ```php > > mutable public string $bar; > > ``` > > > > Hence I mean: currently we have one default option (mutable properties) > > from 2 options. Why do we need to create such a long keyword just to use > > readonly properties (if immutability is better than mutability)? > > > > Regards, Eugene > > A) Please don't top post. This is a bottom-post-centric list. > > B) As others have said, if we were designing the language today we would > do it very differently. I'd likely push for following Rust's example of > immutable by default, super strong typing, and a `mut` keyword to poke > holes in the immutability where necessary. > > However, we have ample existing code that cannot be broken, so that limits > what we can do. That means adding a readonly/immutable/whatever flag is > the only real option, even if it is annoyingly verbose. > > I can see the logic behind "initialize at runtime constants" which is what > you're describing, as an alternative. However, that breaks a ton of > existing patterns. > > 1) Variables in PHP always begin with $. For all time, Always. > > 2) Objects do not have constants. Classes have constants. Introducing > object-level constants is potentially confusing. > > 3) Constants shouldn't be set even once. Properties can be, but > properties have $ on them. > > 3) What would reflection do with these object constants? Are they still > properties according to reflection, or are they something else? Can I > enumerate mutable properties and constant properties together or > separately? This gets complicated fast. > > I agree with the point that the verbosity of `readonly` is potentially > annoying, but we haven't seen it in practice yet. If it ends up being a > problem, then as someone else suggested a more likely solution is a > `readonly` flag on the class, possibly with a `mut` keyword on the property > to opt it back out, or something like that. Not ideal, but probably the > best we could do. > > Having very subtly different syntax for a very significant behavioral > difference in properties seems like a landmine waiting to happen that would > only confuse people. > > --Larry Garfield > > -- > PHP Internals - PHP Runtime Development Mailing List > To unsubscribe, visit: https://www.php.net/unsub.php > > --00000000000046c5dc05c74bf64f--