Re: [RFC] [Discussion] Clone with

From: Date: Tue, 18 Apr 2023 16:48:00 +0000
Subject: Re: [RFC] [Discussion] Clone with
References: 1 2 3  Groups: php.internals 
Request: Send a blank email to internals+get-120068@lists.php.net to get a copy of this message
Hi On 4/18/23 17:45, Larry Garfield wrote:
I agree with the discussion of Nicolas's alternative, which has its merits but also some possible limitations. My only major concern myself is the potential confusion between when to use : and when to use =>. Using the => version only for dynamic keys feels very clunky. In fact, I think the RFC is inconsistent on this front: $object = clone $object with {"foo" => 1}; // the property name is a literal $object = clone $object with {strtolower("FO") . "o" => 1}; // the property name is an expression $object = clone $object with {PROPERTY_NAME => 1}; // the property name is a named constant I would expect the first one to be a colon, not fat-arrow. Is that a typo?
No, that is correct. Colon takes a bare identifier, fat-arrow takes an expression (that just happens to be a constant expression in that case).
Is there a technical reason why they can't just all use a colon? That would extend more nicely to supporting dynamic keys in the future for named arguments.
I'd rather see only the fat-arrow being allowed. Unless I'm missing something, braces with colon is not used anywhere else, whereas braces + '=>' is known from match() and '=>' more generally is already used with array literals [1]. Having two similar syntaxes for the same thing is not great when both are commonly needed is not great. They need to be documented and learned by developers. Best regards Tim Düsterhus [1] In fact if the right hand side of with may be an expression that evaluates to an array, folks wouldn't need to learn new syntax at all:
    $newProperties = [ "foo" => "bar" ];
    clone $object with $newProperties;
and
    clone $object with [ "foo" => "bar" ];


« previous php.internals (#120068) next »