Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:97093 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 70061 invoked from network); 20 Nov 2016 19:25:16 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 20 Nov 2016 19:25:16 -0000 Authentication-Results: pb1.pair.com header.from=rasmus@mindplay.dk; sender-id=unknown Authentication-Results: pb1.pair.com smtp.mail=rasmus@mindplay.dk; spf=permerror; sender-id=unknown Received-SPF: error (pb1.pair.com: domain mindplay.dk from 209.85.217.171 cause and error) X-PHP-List-Original-Sender: rasmus@mindplay.dk X-Host-Fingerprint: 209.85.217.171 mail-ua0-f171.google.com Received: from [209.85.217.171] ([209.85.217.171:34384] helo=mail-ua0-f171.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id B2/80-65233-B98F1385 for ; Sun, 20 Nov 2016 14:25:16 -0500 Received: by mail-ua0-f171.google.com with SMTP id 51so210613768uai.1 for ; Sun, 20 Nov 2016 11:25:15 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=mindplay-dk.20150623.gappssmtp.com; s=20150623; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :cc; bh=cAe7lorcRO3ViLZiiME2eKXNhNieg6doZv6byysECZM=; b=uo60iyp+wDDjeznlhnb80Sf4C0ULwKnPmGBWejUxMf029Jl9Loo82c39KSfEyz5tSP J+VxG3WggbdTMOf6OMA9vHdK/H3AIVw5AZ4P4ZtAB2XrrbJ2Kalop2gHFCCK2G07tmoc 0Zq5rVns4YMtzbnZ+5zQK4vCQC/yseYuYlld5yIK1yDfb9uHXvMhCSp/V85W3z2zhVkK gnUU8vNhkbQGbep1A3+V82VG+bfspjXqht+q0V/Nq70cyLPBWTtcDItFMVJSl9IdrbdI D6+YMxaI8QBY6ey3iWyyiO3tzJ/jIsxDGOst5Jh/ci7RC3FuT0paCOill1GWowX2xO5g P9yw== 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:from:date :message-id:subject:to:cc; bh=cAe7lorcRO3ViLZiiME2eKXNhNieg6doZv6byysECZM=; b=fhLgNz+dsO6fGY/Y178cVbJIGBqudMW1A+U4ZASX0gpzVLxpVTHBfHp6O+IKJ3NVPQ zCfaWewphKgEJHOECrhICFoi+oFapwJKjR8T5hfsyekoS22J/aTZc4QX2Yo8macLGq/D lC7RmVFZnkV8n7FG/tuIiEyOTr9Ilg/xdOXUlgPJn0WOpcsfo57n0T6o9wKD1XWtMu+0 ngNrtGoXPKLTFvieEfQfvG9Ljpr3LkcTBOH02pZNlvBAHeRiuUz1OdtGFlf6Lc0E7GtX Bj3ImIWCkY9R7aLwZux2l+y5xoa5PtbjHeUVNmt4hmUXGqrCykcNyduKYAfyU2OAb7qi kvyg== X-Gm-Message-State: AKaTC034okQoPjKAmT/7wOlzo5uwokm7OJEF0wNn/ItrNceKQVzi328qTvB5RkeJ0TzPF87YaWiR24MXosuTKQ== X-Received: by 10.176.5.130 with SMTP id e2mr5551945uae.108.1479669912516; Sun, 20 Nov 2016 11:25:12 -0800 (PST) MIME-Version: 1.0 Received: by 10.103.1.147 with HTTP; Sun, 20 Nov 2016 11:25:11 -0800 (PST) In-Reply-To: References: Date: Sun, 20 Nov 2016 20:25:11 +0100 Message-ID: To: =?UTF-8?Q?Silvio_Mariji=C4=87?= Cc: PHP Internals List Content-Type: multipart/alternative; boundary=94eb2c123e1c8552bc0541c07f75 Subject: Re: [PHP-DEV] Immutability RFC From: rasmus@mindplay.dk (Rasmus Schultz) --94eb2c123e1c8552bc0541c07f75 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable Reading through the RFC and the replies, I'm confused about this question. Why would the comparison operators work any differently on immutable objects? If these were value objects, the question would make sense, but it doesn't sound like that's what you're proposing? With regards to the feature itself, I frankly don't feel like it makes any sense. I disagree with the entire premise of the RFC. > Currently PHP lacks native support for immutability. No, it doesn't. You show several examples of immutable classes in the RFC, then demonstrate a slightly more abbreviated syntax to accomplish the same thing. > Because of that user-land applications are using third party libraries or resort to custom implementations and still there is no easy enforcement of immutability. Really? There are third-party libraries for immutable classes? .... Why? It's true that there is no way for a class, internally, to enforce immutability on itself - but why would you need to do that? You're writing the class. If you want immutable state inside your class, simply don't change the state. Why would you need the language to enforce rules that you can already enforce with the language? > Introducing this feature would help bring one unified solution to this problem, and also it would remove unnecessary logic from user-land applications. What is this unnecessary logic you're referring to? Get-methods generally do not contain logic. There is no more or less logic in the examples you show in your before/after code samples. Perhaps you mean boilerplate? So yes, this feature removes a little bit of boilerplate. Writing immutable classes becomes a little less ceremonious. I think that the problem of writing get-methods is exaggerated and somewhat contrived. This feature doesn't enable you to do something you couldn't do before - it obviates the need to write get-methods, but that's basically all, and I'm not even convinced that's a good thing. Public properties, in practice, are almost never used in PHP code. You very rarely see anybody use them, because they're not generally safe, e.g. permits client code to inject values of the wrong type, etc. What you're proposing will make public properties the default for immutable objects. This will lead to inconsistencies - for example: immutable class Name { public $first; public $last; public function getFullName() { return "{$this->first} {$this->last}"; } } This will get confusing quickly - you will have to learn or memorize which attributes of your model are available as properties or methods, e.g. $name->first is pretty far removed from $name->getFullName() both in terms of syntax and style. I think this will become quite confusing in practice, as nobody currently expects or looks for public properties. Also, having to refactor from a get-method to a public property, or vice-versa, becomes a chore that causes a breaking change. The term 'immutable", especially for the property-annotation, doesn't strictly seem correct to me either, as for example, I can annotate a public property as immutable, but if I put an object in there, that object is not necessarily immutable. The correct term for this feature, especially for properties, I would think is "readonly" - which would be consistent with the description of certain existing properties in PHP APIs, such as PDOStatement::$queryString, as well as with other languages such as C#. Either way, this feature proposes to introduce "asynchronous" properties as a new concept in PHP, where properties are generally "synchronous", in the sense that they can always be read and written. Other languages that support asynchronous properties (C#, Objective-C, Dart, probably many others) typically leverage that concept for more than just simple immutability - the concept of properties that behave differently when you attempt to read and write is not typically a mere annotation/directive instructing the compiler to enforce a rule, it's typically made possible via support for accessors. This might limit future options for the language, perhaps depending on how it's implemented. I hope it doesn't mean we can't have accessors at some point, in the distant future. In my opinion, all of these things go hand-in-hand: accessors, read-only, property type-hints, as these are all essentially about defining and enforcing constraints and making assertions about the state of a model. I feel that this feature is a poor substitute for accessors, for example, because this feature really applies only to immutable models - whereas a feature such as accessors could be used equally to create immutable models, but would permit us to move away from get/set-methods entirely, e.g. would allow to write models with more consistent APIs, for example something like= : class Name { public readonly $first; public readonly $last; public $full_name { get { return "{$this->first} {$this->last}"; } } } Something like that makes for more consistent client code, e.g. $name->first and $name->full_name, etc. I'd like to see us adding features that work everywhere, for many use-cases, mutable and immutable - introducing a language feature that works only for immutable objects just feels to exotic to me, in a language that is mutable by default. I'd prefer to see features with broader applications. On Wed, Nov 16, 2016 at 2:57 PM, Silvio Mariji=C4=87 wrote: > Hi, > > To anyone who is interested in this RFC. What do you think what behavour = we > should have when you try to compare two immutable objects by identity lik= e > this: > > immutable class A { > > public $a; > > public function __construct($a) { > $this->a =3D $a > } > > } > > $a1 =3D new A(1); > $a2 =3D new A(1); > > $a1 =3D=3D=3D $a2 > > If we treat those objects as values then this should return true. But the= n > again there might be some confusion because then two operators are doing > the same thing. Maybe throw an error ? Suggestions ? > > Cheers. > -- > Silvio Mariji=C4=87 > Software Engineer > 2e Systems > --94eb2c123e1c8552bc0541c07f75--