Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:114986 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 46065 invoked from network); 21 Jun 2021 19:38:12 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 21 Jun 2021 19:38:12 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 061631804C3 for ; Mon, 21 Jun 2021 12:56:17 -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.6 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,RCVD_IN_DNSWL_LOW,SPF_HELO_PASS,SPF_NONE autolearn=no autolearn_force=no version=3.4.2 X-Spam-Virus: No X-Envelope-From: Received: from out3-smtp.messagingengine.com (out3-smtp.messagingengine.com [66.111.4.27]) (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, 21 Jun 2021 12:56:16 -0700 (PDT) Received: from compute1.internal (compute1.nyi.internal [10.202.2.41]) by mailout.nyi.internal (Postfix) with ESMTP id F30A25C00FD for ; Mon, 21 Jun 2021 15:56:14 -0400 (EDT) Received: from imap43 ([10.202.2.93]) by compute1.internal (MEProxy); Mon, 21 Jun 2021 15:56:14 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=content-type:date:from:in-reply-to :message-id:mime-version:references:subject:to:x-me-proxy :x-me-proxy:x-me-sender:x-me-sender:x-sasl-enc; s=fm3; bh=KrGXHt sHlG6rLn6c3UhSOKVn200STrBrVQ6tHMZNadQ=; b=S/stJd3plKo4iIeEiUSh/x IRuWQrWDWQLOoCvMDiVQqPqFEHJhx5EV2exsFIP1/94/qvjMDY2imdiBXkDRzEtb RCLxehL4urkX12gnagZ1sYeP7NMRdb3MBMOuJ8ImN4rMcB14ZT5TtBIJggBmZVKs GohQnd7kul+yOm17cRivbKRTfUOGlBvh95B9UIu4rsfsuqAODEc8qqhXCu+Qh0OG ApbasuZdwik/l138Vim2GD0lEqhc6Zp/hJYTlpG7ZkdMJ4o8BUnk0V2QNrpSR0xl RxBbYZiS2ctJLrH4HHZoLgMmm3zNaot29ps1VitwZyi9/oHQyHKDOLFw1MfyJe9A == X-ME-Sender: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgeduledrfeefledgudegudcutefuodetggdotefrod ftvfcurfhrohhfihhlvgemucfhrghsthforghilhdpqfgfvfdpuffrtefokffrpgfnqfgh necuuegrihhlohhuthemuceftddtnecusecvtfgvtghiphhivghnthhsucdlqddutddtmd enucfjughrpefofgggkfgjfhffhffvufgtsehttdertderredtnecuhfhrohhmpedfnfgr rhhrhicuifgrrhhfihgvlhgufdcuoehlrghrrhihsehgrghrfhhivghlughtvggthhdrtg homheqnecuggftrfgrthhtvghrnhepveehhedvveejledvvefgleevffdtjeekledvkeeg heffgfeivdejhffhledtudetnecuffhomhgrihhnpehphhhprdhnvghtnecuvehluhhsth gvrhfuihiivgeptdenucfrrghrrghmpehmrghilhhfrhhomheplhgrrhhrhiesghgrrhhf ihgvlhguthgvtghhrdgtohhm X-ME-Proxy: Received: by mailuser.nyi.internal (Postfix, from userid 501) id B1852AC0072; Mon, 21 Jun 2021 15:56:14 -0400 (EDT) X-Mailer: MessagingEngine.com Webmail Interface User-Agent: Cyrus-JMAP/3.5.0-alpha0-530-gd0c265785f-fm-20210616.002-gd0c26578 Mime-Version: 1.0 Message-ID: In-Reply-To: <7f2da982-e29b-ccca-bddc-24ae7f4b0390@gmail.com> References: <7f2da982-e29b-ccca-bddc-24ae7f4b0390@gmail.com> Date: Mon, 21 Jun 2021 14:55:54 -0500 To: "php internals" Content-Type: text/plain Subject: Re: [PHP-DEV] [PROPOSAL] Bare name array literals (again) From: larry@garfieldtech.com ("Larry Garfield") On Mon, Jun 21, 2021, at 12:34 PM, Rowan Tommins wrote: > On 21/06/2021 17:54, tyson andre wrote: > > In every place where `key` is a valid php identifier > > (e.g. can be used in PHP 8.0's named parameters), > > I propose to allow `[key: expr]` to be used instead of `['key' => expr]`. > > > This is an immediate "no" from me: it multiplies the ways to write the > same thing from 2 to 4, in order to save a few bytes, in a few instances. > > I think this is something that Douglas Crockford got absolutely right > when he simplified JavaScript object syntax to formalise JSON: every > valid key can be represented as a quoted string, so if the quotes are > always there, you don't need to remember a list of rules about reserved > words, allowed characters, etc. > > > > This is useful for shortening long arrays where the keys are known literals, > > e.g. > > > > ```php > > return [success: true, data: $data, cursor: $cursor]; > > // is equivalent to the following, but shorter: > > return ['success' => true, 'data' => $data, 'cursor' => $cursor]; > > ``` > > > Although common, this is not a good use of arrays; if your keys are > "known literals", they should be fields of some object: > > return new Result(success: true, data: $data, cursor: $cursor); > > > If you don't want to declare a class (yet), you can use an anonymous > object. Rather than yet another way to write arrays, it would be great > to have some more powerful syntax for those; currently you'd have > something like this: > > return new class(success: true, data: $data, cursor: $cursor) { public > function __construct(public bool $success, public array $data, public > CursorInterface $cursor) {} }; > > > Brainstorming, we could perhaps extend property promotion into the "new > class" clause, so that you could write this: > > return new class(public bool success: true, public array data: $data, > public CursorInterface cursor: $cursor) {}; I agree with Rowan on both points. The issues this would supposedly solve a better solved by continuing to work on making real-objects easier to work with. PHP 8 made huge strides in that area. Making anon classes easier would be the next logical step. Also, Mel mentioned destructuring. Ilija has partially-finished work on a pattern matching operator that would include destructuring here: https://wiki.php.net/rfc/pattern-matching It's not going to make it into 8.1, sadly, but hopefully that or something like it would make it into 8.2. --Larry Garfield