On Wed, Dec 30, 2020, at 12:42 PM, Olle Härstedt wrote: > A more motivating example for uniqueness is perhaps a query builder. > > ``` > $query = (new Query()) > ->select(1) > ->from('foo') > ->where(...) > ->orderBy(..) > ->limit(); > doSomething($query); > doSomethingElse($query); > ``` > > In the above snippet, we don't know if doSomething() will change > $query and cause a bug. The issue can be solved with an immutable > builder, using withSelect(), withWhere(), etc, OR it's solved with > uniqueness, forcing a clone to avoid creating a new alias (passing > $query to a function creates an alias inside that function). The > optimisation is the same as in my previous example, avoiding copying > $query multiple times during build-up.
For a query builder, I probably wouldn't make it immutable anyway, myself. If you really want to force that doSomething() cannot modify the object that is otherwise mutable, calling doSomething(clone $query) already works today and gets that net effect, provided that Query is safe to clone. (Vis, has no service dependencies, and if it has any dependent value objects then it has a __clone() method that deep clones.) > >> The guarantee in both above snippets is that myfun() DOES NOT modify > >> $b before returning it. BUT with immutability, you have to copy $b > >> three times, with uniqueness only one. Yes, but with CoW those 3 copies are not that expensive, so we can most of the time ignore them except as a very micro-optimization. (See previous email.) --Larry Garfield -- PHP Internals - PHP Runtime Development Mailing List To unsubscribe, visit: https://www.php.net/unsub.php