Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:109063 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 71835 invoked from network); 16 Mar 2020 17:18:00 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 16 Mar 2020 17:18:00 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 50E5A1804DD for ; Mon, 16 Mar 2020 08:40:27 -0700 (PDT) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on php-smtp4.php.net X-Spam-Level: X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM,HTML_MESSAGE, RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS autolearn=no autolearn_force=no version=3.4.2 X-Spam-ASN: AS15169 209.85.128.0/17 X-Spam-Virus: No X-Envelope-From: Received: from mail-il1-f170.google.com (mail-il1-f170.google.com [209.85.166.170]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange ECDHE (P-256) server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by php-smtp4.php.net (Postfix) with ESMTPS for ; Mon, 16 Mar 2020 08:40:26 -0700 (PDT) Received: by mail-il1-f170.google.com with SMTP id l14so16921418ilj.8 for ; Mon, 16 Mar 2020 08:40:26 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:references:in-reply-to:from:date:message-id:subject:to; bh=j5Q3Zc3089l+hG+nAEcO9Q6Zca3nfEwo8Rp3qi4rgqk=; b=Xd2NYicNyoBHGbX9z052RI/Xj3+YKAhHH+EESApnhpKLwTw6ZzdPyd+vxP+rKXkj+z T+X/j5cZf+L4zzqX3ynEeFr1fcBrZhv9jcAEecmrd/W8ZFVt8WoBxWT1+MSO6czo2kGo U95QFs2wVcD8bYkc8SWHGDY3rg+Dwy02PWAA9ygrZNJmmYED6ZyLMRWjhn22+XGHEPft Y/5x5JLiZ/omfRqh9LqVW0IiKvK22ynjzm4Bnt2pevMoDZu1fs6HNDDg+Mk/uPTtAm4u Yy0sURZyJ1OlhVjMhrmPsgWVapk1qZiTnm+fwbSwMnk7XscVGfJkAbXGl5sLHo71T2eo 5OJw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to; bh=j5Q3Zc3089l+hG+nAEcO9Q6Zca3nfEwo8Rp3qi4rgqk=; b=Hs9Tfbawb+5IXqzh3W8y3UuS3K63BhBtdpq+67f5cvmicga8pE44GNta50iJA98v2D bZv9UA8Div3Yp067kL//GL8Z2j/EhDt3kmS5iHnf/WRgRKLfvqYO/xF34BMCV8C7GBvM aqehNKRlsGtpZmQE4ZZ8aL3CHYDozZuqTbyDMamhu5AUpyQkfXkVGkkZnbBaDxihH5wK XqLXPwoKJZj624VOdYHcHAL87er8YuLHK+wQMIelG722O8cXe+aUvSrsF30tZ8eoPCHp XrDTEgz4GxU6wdqb9SvvYNMlxirjaSznnI6cgYBboHE4yPDq2LxVnbh2b3rmGw0T5z02 t3ZA== X-Gm-Message-State: ANhLgQ1K5Zc9cpRJWKiszgRSwiJyLzQ2qifcytF+FQGu61IkrRm+BorT JRGn2+np8gJSuV22d7ioMH2/WdVQ0qVYtVGL4KwQhDCs X-Google-Smtp-Source: ADFU+vvEnC+jkwQlFuEiiHNAlRvLkwVSVe6XJJMK5JAOLb1RSPLm2jgJqeXyxL48gCLl5UVg3cAE6toeUgVDxPmBjmY= X-Received: by 2002:a92:3b11:: with SMTP id i17mr369527ila.161.1584373222970; Mon, 16 Mar 2020 08:40:22 -0700 (PDT) MIME-Version: 1.0 References: In-Reply-To: Date: Mon, 16 Mar 2020 15:40:11 +0000 Message-ID: To: php internals Content-Type: multipart/alternative; boundary="00000000000025a32e05a0faa3a5" Subject: Re: [PHP-DEV] [RFC] [DISCUSSION] Compact Object Property Assignment From: rowan.collins@gmail.com (Rowan Tommins) --00000000000025a32e05a0faa3a5 Content-Type: text/plain; charset="UTF-8" On Mon, 16 Mar 2020 at 11:48, Jakob Givoni wrote: > https://wiki.php.net/rfc/compact-object-property-assignment > - A pragmatic approach to object literals > > Let me know what you think! > Hi Jakob, Thanks for having another go at this feature, which I think a lot of people would like in some form. I think I agree with Matthew that the almost-but-not-quite array syntax looks a bit odd, but that's something that can be worked on if we agree the basic idea is sound. I fear that the need to directly set public properties (or use __set) would limit the use cases of this somewhat. I can see two ways we could arrive at something more broadly useful: * A syntax like this, plus better support for property setters (inline like in C# etc, rather than needing __set) * Named parameters plus short-hand constructor definitions (e.g. function __construct($this->foo) removing the need to type $this->foo = $foo) Maybe we want to have all four, but that would leave us with a lot of different ways of initialising objects, which might not be such a good thing. You mention using "argument bag" objects in place of named parameters, but that feels like it would be even more awkward than using an associative array. To get the example in the RFC doing something useful, you'd need quite a lot of boilerplate: class FooOptions { string $mane; string $hum; } class Foo { string $mane; string $hum; public function __construct(FooOptions $options) { // Still have to check for uninitialized properties, since COPA won't enforce them if ( ! isset($options->mane) || ! isset($options->hum) ) { throw new Exception('Bad options'); } // Still have to copy parameters one at a time $this->mane = $options->mane; $this-> hum = $options->hum; } } $myFoo = new Foo((new FooOptions())->[ mane = 'get', hum = 'life', ]); Compare that to named parameters (picking a syntax that looks similar to yours), even without any new constructor syntax: class Foo { string $mane; string $hum; public function __construct(string $mane, string $hum) { // Type and mandatory param checks are automatic // Still have to copy parameters one at a time, unless we $this->mane = $mane; $this-> hum = $hum; } } $myFoo = new Foo([ mane = 'get', hum = 'life', ]); Writing that out also made me realise a key difference between this and constructor short-hands, which is that COPA can only validate individual parameters, not the whole object; so it's hard to complain if the user forgets a mandatory field. Sometimes that wouldn't matter, but again, it limits the cases where this syntax would be useful, even if we had property setters. Regards, -- Rowan Tommins [IMSoP] --00000000000025a32e05a0faa3a5--