On Thu, Apr 20, 2023 at 9:01 AM Rowan Tommins <rowan.coll...@gmail.com> wrote:
>
> On 18 April 2023 17:48:00 BST, "Tim Düsterhus" <t...@bastelstu.be> wrote:
> >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.
>
>
> I think it makes sense to have an unquoted form here, because the common case 
> is that they are names which analysers can match statically to particular 
> properties, not strings which will be analysed at runtime. There are plenty 
> of places in the language where dynamic names are allowed, but we don't just 
> use strings for the static case:
>
> ${'foo'} = 'bar'( constant('BAZ') )->{'quux'}();
>
>
> More specifically, the "name: $value" syntax matches named parameters, and 
> while you can use an array for that (via ... unpacking), we don't force users 
> to do so.
>
> In fact, the Future Scope of that RFC considered the opposite: using the 
> colon syntax in arrays, along with a shorthand for pulling the key name from 
> the variable name: 
> https://wiki.php.net/rfc/named_params#shorthand_syntax_for_matching_parameter_and_variable_name
>
> >  If I wanted to put these ideas into a general framework, I think one way 
> > to go about this would be as follows:
> >
> > - Consider identifier: $expr as a shorthand for "identifier" => $expr.
>  > - Consider :$variable as a shorthand for variable: $variable and thus 
> "variable" => $variable.
>
>
> That would give us:
>
> $point = ['x' => $x, 'y' => $y, 'z' => $z];
> $point = [x: $x, y: $y, z: $z];
> $point = [:$x, :$y, :$z];
>
> $point = new Point(...['x' => $x, 'y' => $y, 'z' => $z]);
> $point = new Point(x: $x, y: $y, z: $z);
> $point = new Point(:$x, :$y, :$z);
>
> $point = clone $point with {'x' => $x, 'y' => $y, 'z' => $z};
> $point = clone $point with {x: $x, y: $y, z: $z};
> $point = clone $point with {:$x, :$y, :$z};
>
>
>
> Rather than making everything use an array or array-like syntax, I would 
> probably go the other way and scrap the special syntax for dynamic names, 
> making the whole thing look like a function call, with support for array 
> unpacking:
>
> $point = clone $point with (x: $x, y: $y, z: $z);
> $point = clone $point with (...['x' => $x, 'y' => $y, 'z' => $z]);
>
>
>
> Regards,
>
> --
> Rowan Tommins
> [IMSoP]
>
> --
> PHP Internals - PHP Runtime Development Mailing List
> To unsubscribe, visit: https://www.php.net/unsub.php
>

I think using arrays here makes a lot of sense.

For example, this example:

public function withStatus($code, $reasonPhrase = ''): Response
{
    return clone $this {
        $this->statusCode = $code;
         $this->reasonPhrase = "Old: $this->reasonPhrase, New: $reasonPhrase";
    }
};

could be rewritten with

public function withStatus($statusCode, $reasonPhrase = ''): Response {
  // perform validation here
  $reasonPhrase = "Old: $this->reasonPhrase, New: $reasonPhrase";
  return clone $this with compact('statusCode', 'reasonPhrase');
}

I, personally, would find this much more ergonomic than writing out
blocks of code and having a totally different syntax.

--
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: https://www.php.net/unsub.php

Reply via email to