Re: Readonly properties - immutability by default

From: Date: Fri, 16 Jul 2021 10:02:59 +0000
Subject: Re: Readonly properties - immutability by default
References: 1 2 3  Groups: php.internals 
Request: Send a blank email to internals+get-115434@lists.php.net to get a copy of this message
Le 16/07/2021 à 10:11, Eugene Sidelnyk a écrit :
Thanks for your response! Anyway, I probably put it wrong by saying "by default", so let me clarify myself. What I really mean is omitting the dollar sign. So everything remains the same with ordinary properties (which are mutable), and we introduce immutable (readonly) properties as another type of them. It looks like a great default:
     public string name;
So, once again, it has nothing to do with backward compatibility. No one disposes the way properties are currently working. We introduce a new _type_ or _kind_ of properties - readonly. Also I see useful future scope like readonly parameters:
function foo(int firstParam, bool secondParam)
{
   // no way to modify it, Error
   firstParam = 23 * secondParam;

   return firstParam * secondParam;
}
Yes, we can implement this with another keyword (again, readonly), but as I see, only few people will use it because it is too complicated (really, instead of simply declaring an argument, a programmer has to write a bunch of other stuff in front of it for every single method and function).
Hello, I'm very happy that the readonly properties RFC did pass, I waited for this for a long time. I like the "readonly" explicit keyword, explicit is always better than implicit for disambiguation and readability. I'd wish having per-default readonly properties as well, but this is PHP and fundamentals cannot be changed just like that, it's not Rust, or any other language, PHP is PHP. Explicit "readonly" is in my opinion the best option, it's easy to type, and it's easy to read, and it's the same keyword in many other languages. On the opposite side, omiting the $ sign seems like a huge mind-fuck, easy to miss-read, easy to miss-type, error prone and obscure. Regards, -- Pierre

« previous php.internals (#115434) next »