2015-11-16 10:40 GMT+01:00 Chris Riley <t.carn...@gmail.com>: > > > On 16 November 2015 at 09:33, Lorenzo Fontana <fontanalor...@gmail.com> > wrote: > >> I really like the concept of immutability, but I think that it should be >> applicable at instance level rather than declaration. >> >> I would also prefer another keyword than immutable. >> >> Final does not make the properties immutable, it makes the class not >> extensible. >> On Nov 16, 2015 10:24, "Daniel Persson" <mailto.wo...@gmail.com> wrote: >> >>> Any differance from the final keyword? >>> >>> http://php.net/manual/en/language.oop5.final.php >>> >>> On Mon, Nov 16, 2015 at 10:15 AM, Chris Riley <t.carn...@gmail.com> >>> wrote: >>> >>> > Hi, >>> > >>> > 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: >>> > >>> > immutable class Foo { >>> > public $bar; >>> > public function __construct($bar) { >>> > $this->bar = $bar; >>> > } >>> > } >>> > >>> > Immutable on a class declaration makes all (maybe only public?) >>> properties >>> > of the class immutable after construct; assigning to a property would >>> > result in a Fatal error. >>> > >>> > class Foo { >>> > public $bar; >>> > immutable public $baz; >>> > } >>> > >>> > Immutable on a property makes the property immutable once it takes on a >>> > none null value. Attempts to modify the property after this results in >>> a >>> > fatal error. >>> > >>> > Any thoughts? >>> > ~C >>> > >>> >> > What instance level syntax would you propose? > > $foo = new immutable bar(); ? > > or > > immutable $foo = new bar(); > > Gives less flexibility imo and less guarantees of correctness. > > Consider the current user land implementation of immutable classes: > > class Foo { > private $bar; > public function __construct($bar) { > $this->bar = $bar; > } > > public function getBar() { > return $this->bar; > } > } > > Is already done at declaration declaration based immutability is probably > more desirable. > > What keyword would you suggest other than immutable? > > ~C >
I would prefer immutable $foo = new bar(); that can also be applied to: immutable $bar = "string"; immutable $baz = 1; immutable $arr = [1, 2, 3]; and not only to classes. The "userland implementation of immutable classes" you are proposing is not immutable at all. First of all one could define another accessor method that can change the state or can bind a closure to $this and change an internal property at runtime. Some possible keywords that "means" immutable to me are: let $bar = "string"; val $bar = "string"; What do you think?