Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:89256 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 65188 invoked from network); 17 Nov 2015 07:18:55 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 17 Nov 2015 07:18:55 -0000 Authentication-Results: pb1.pair.com smtp.mail=t.carnage@gmail.com; spf=pass; sender-id=pass Authentication-Results: pb1.pair.com header.from=t.carnage@gmail.com; sender-id=pass Received-SPF: pass (pb1.pair.com: domain gmail.com designates 74.125.82.42 as permitted sender) X-PHP-List-Original-Sender: t.carnage@gmail.com X-Host-Fingerprint: 74.125.82.42 mail-wm0-f42.google.com Received: from [74.125.82.42] ([74.125.82.42:36523] helo=mail-wm0-f42.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 96/60-57591-DD4DA465 for ; Tue, 17 Nov 2015 02:18:54 -0500 Received: by wmww144 with SMTP id w144so140902145wmw.1 for ; Mon, 16 Nov 2015 23:18:50 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :content-type; bh=Xd6ALDSOZzQ9pBbzmLAEpIfEsiJOBNemKDrJ8vyZfCs=; b=GtG1XDkegfVG45dE1ZAwe9e3/ANKf6MX7M6umq0NPST4lJvSe0BKx1RTzvNNypy2in C8CsfqwE8G4l2pmVcDA7ADQC2NOnEvfKnZ6M0ocw4hHM9wpo5ebDm7eqV7Vhx00nV9GH 36qmZvKXLjp0sQGWK4x9IH+97jkDwpYrQJ/TTfIYUOAzaP2phJXvWpEA2FQOnE5hvjKU UtKEZjlKt9zywRFeHc+0m9zd+YMnnJ/aFA2eV3dOLCNDWCsVCnR9HF/QbEfCcjbwsTxV o+UlWR0fVuQGx4YorsaJ41YvPtoeQ4w6kOI1lfkAB9Gsm0yTQpvDuHG8Ce1q9/8oE8qR 3jiA== X-Received: by 10.28.18.9 with SMTP id 9mr683602wms.96.1447744730186; Mon, 16 Nov 2015 23:18:50 -0800 (PST) MIME-Version: 1.0 References: In-Reply-To: Date: Tue, 17 Nov 2015 07:18:40 +0000 Message-ID: To: Andrea Faulds , internals@lists.php.net Content-Type: multipart/alternative; boundary=001a1145bd105e4a260524b756b8 Subject: Re: [PHP-DEV] Immutable modifier From: t.carnage@gmail.com (Chris Riley) --001a1145bd105e4a260524b756b8 Content-Type: text/plain; charset=UTF-8 On Tue, 17 Nov 2015, 02:07 Andrea Faulds wrote: Hi, Chris Riley wrote: > > There has been a lot of interest recently (eg psr-7) in immutable data. I'm > considering putting an RFC together to add language support for immutables: > I wonder if immutable classes are really the right way to go. Immutable reference types with manual copying are somewhat alien to PHP: instead, the language has used copy-on-write mutable value types for arrays and strings, and prior to PHP 5, objects. Copy-on-write value types have all the benefits immutable types do. They can't be mutated at a distance unless you make a reference to them, trees made of them can be compared for equality with just ===, etc. But they also have benefits that immutable types lack. They're easier to work with, because you can directly mutate them like everyone's used to doing: no messing around with withFooBar() and withoutFooBar(), you just set foobar or remove it directly. And PHP handles the duplication of objects for you implicitly, so there's far less code to write. Unfortunately, PHP 5 got rid of PHP 4's value type objects and replaced them with reference type objects. But we could always introduce a way to get value type objects again. How about a `struct` keyword? It would be equivalent to `class`, but produce a value type rather than a reference type. Any thoughts? Thanks. -- Andrea Faulds http:// ajf.me/ -- PHP Internals - PHP Runtime Development Mailing List To unsubscribe, visit: http:// www.php.net / unsub.php My main motivation for this was for event objects when doing event sourcing - they are by definition unchangeable after creation. That said, considering the wider use case there does seem to be a need to emulate the with* functions. One option would be to hide the messy constructor call within user defined methods, but that would add a lot of boilerplate - something I was wanting to reduce not increase. I can't think of a clean easy way to add with* functionality at a language level without even more magic (parameter skipping?) As for setter injection - my initial proposal would support that - a property declared immutable allows itself to be set - once. If someone can give my wiki account rfc karma I can write this up so far too help focus discussion. ~C --001a1145bd105e4a260524b756b8--