Re: [PHP-DEV] [RFC] [DISCUSSION] Compact Object Property Assignment

This is only part of a thread. view whole thread
  109060
March 16, 2020 14:30 ocramius@gmail.com (Marco Pivetta)
Hey Jakob,

What happens if you have an expression that throws?

class Foo
{
    public $a;
    public $b;
    public $c;
}

$instance = new Foo();

function iThrow() {
    throw new \Exception();
}

try {
    $foo ->[
        a = 'a',
        b = iThrow(),
        c = 'c',
    ];
} catch (\Throwable $e) {
    var_export($foo); // ???
}

What will the state of `$foo` be here?

Marco Pivetta

http://twitter.com/Ocramius

http://ocramius.github.com/


On Mon, Mar 16, 2020 at 12:48 PM Jakob Givoni <jakob@givoni.dk> wrote:

> Hello Internals, > > I'm opening up my new RFC for discussion: > > https://wiki.php.net/rfc/compact-object-property-assignment > - A pragmatic approach to object literals > > Let me know what you think! > > Best, > Jakob > > -- > PHP Internals - PHP Runtime Development Mailing List > To unsubscribe, visit: http://www.php.net/unsub.php > >
  109086
March 17, 2020 01:26 jakob@givoni.dk (Jakob Givoni)
On Mon, Mar 16, 2020 at 9:31 AM Marco Pivetta <ocramius@gmail.com> wrote:
> What happens if you have an expression that throws? > > class Foo > { > public $a; > public $b; > public $c; > } > > $instance = new Foo(); > > function iThrow() { > throw new \Exception(); > } > > try { > $foo ->[ > a = 'a', > b = iThrow(), > c = 'c', > ]; > } catch (\Throwable $e) { > var_export($foo); // ??? > }
Hi Marco! Trivial question - let's see what happens: Just replace COPA with the old syntax and run it: try { $foo->a = 'a'; $foo->b = iThrow(); $foo->x = 'c'; } catch (\Throwable $e) { var_export($foo); // ??? } Result: Foo::__set_state(array( 'a' => 'a', 'b' => NULL, 'c' => NULL, )) So the first property will be set, the rest will be left as they were. Best, Jakob
  109087
March 17, 2020 01:29 ocramius@gmail.com (Marco Pivetta)
Hey Jakob,

On Tue, Mar 17, 2020, 02:27 Jakob Givoni <jakob@givoni.dk> wrote:

> On Mon, Mar 16, 2020 at 9:31 AM Marco Pivetta <ocramius@gmail.com> wrote: > > What happens if you have an expression that throws? > > > > class Foo > > { > > public $a; > > public $b; > > public $c; > > } > > > > $instance = new Foo(); > > > > function iThrow() { > > throw new \Exception(); > > } > > > > try { > > $foo ->[ > > a = 'a', > > b = iThrow(), > > c = 'c', > > ]; > > } catch (\Throwable $e) { > > var_export($foo); // ??? > > } > > Hi Marco! > Trivial question - let's see what happens: > > Just replace COPA with the old syntax and run it: > > try { > $foo->a = 'a'; > $foo->b = iThrow(); > $foo->x = 'c'; > } catch (\Throwable $e) { > var_export($foo); // ??? > } > > Result: > Foo::__set_state(array( > 'a' => 'a', > 'b' => NULL, > 'c' => NULL, > )) > > So the first property will be set, the rest will be left as they were. >
That kinda makes the entire feature quite useless/uninteresting to me: if a this does is desugaring into a set of assignments, then I'm not sure what its advantage is. I was kinda hoping for an atomic state mutation across multiple fields, heh...
>
  109090
March 17, 2020 01:49 jakob@givoni.dk (Jakob Givoni)
On Mon, Mar 16, 2020 at 8:29 PM Marco Pivetta <ocramius@gmail.com> wrote:
> That kinda makes the entire feature quite useless/uninteresting to me: if a this does is desugaring into a set of assignments, then I'm not sure what its advantage is. I'm sorry about that, but the motivation of the RFC is quite clear in
the "Motivation" section. If those goals are not interesting to you then COPA won't help you.
> I was kinda hoping for an atomic state mutation across multiple fields, heh... Sounds like another RFC (that may make it in PHP 9.x+ ;-D)