Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:116178 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 63876 invoked from network); 27 Sep 2021 11:25:16 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 27 Sep 2021 11:25:16 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 20A2E1804C5 for ; Mon, 27 Sep 2021 05:07:42 -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=-0.2 required=5.0 tests=BAYES_40,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM,HTML_MESSAGE, RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H2,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-io1-f43.google.com (mail-io1-f43.google.com [209.85.166.43]) (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, 27 Sep 2021 05:07:41 -0700 (PDT) Received: by mail-io1-f43.google.com with SMTP id s20so22426656ioa.4 for ; Mon, 27 Sep 2021 05:07:41 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=FcWFjdDIJ07YgDM9IBaltBAD/GOTvsEodUgcHMLtbb0=; b=FeqUO00AU9uMFe3KmG9DT0QgQM3Jt87TSkwfJfHECWABmwgxIBg/lgFc5bvkHAUr8s 1nlje2k7m1MyeO0YGTp/1+gW2nnNJLb3XVXAW8mwukCk+hFXT2sgGe8D9SmBq+4Wry6m 4A7W7fQltHK3DkyYiW4X0Xn5QRxHHGmLAxy8Jb+o6Tg6J4yrhXf0XpSsWDt6SdxwLA6p z/LXzzTmgNkMmuER5lDBSIRccKUFZDyxsEVvWcBp5uwmjYDhlLq6vRwGEGi0NyaVKccC A7BtPQZFVlco8z9vrkLt8feg6Z0BXkJITD71SfICgCa6Z8vOCwbVPdiC1luGoU3XtwMz fVtQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=FcWFjdDIJ07YgDM9IBaltBAD/GOTvsEodUgcHMLtbb0=; b=N03Y6EawifhPxmYRq+DfZrpa7WWB00nqJUC2cm+upHrMnM7t691Bd/w9zUqmPbcJFV svLUdRNlublfN5l2aDw0nIb6dSwyHzRKkQ9re68ekc9dIa41++AVunMppuyvuoqvFbw3 O1yVPh3lytm0SbhqIoBQjZ0tfUDho98hnPgiF6WkrMx3FDKacJebGRuPr4OCyy9moh0N als9feD/uNv2bTcTQ/EuhQmy/pPG2OduPYWRMRNBRt1A3iivLuZXmHiWzo2YKwhx74iX do/x46daao9zWLPuZhkOEPxyvJm3J2QXGcCroQzN2LB2EGbvK3bLWiwZgPynVHo4i6T4 Aprw== X-Gm-Message-State: AOAM532Zf8yXsjzJwNClmUzbQJsUMadVgLBYEasnZ1TwYB9L2r99Ur2Q 32nBtum9FvI2LBrKdr3OWcTtsMXxlYeBN1x57rQSIDI5MmM= X-Google-Smtp-Source: ABdhPJzBym+hM/S+xLKmQNGjUO77PEWgH/d1xlcp1L8eMkC1n09JTCBUGZceEUFuyGRWH+G2l6BpZdqN0u+qh8Wpwmk= X-Received: by 2002:a05:6602:20d3:: with SMTP id 19mr21037072ioz.86.1632744459398; Mon, 27 Sep 2021 05:07:39 -0700 (PDT) MIME-Version: 1.0 References: In-Reply-To: Date: Mon, 27 Sep 2021 14:07:13 +0200 Message-ID: To: Konrad Baumgart Cc: PHP internals Content-Type: multipart/alternative; boundary="00000000000082fbf605ccf8f188" Subject: Re: [PHP-DEV] Proposal: Shorthand initialization and destructuring of associative arrays From: kjarli@gmail.com (Lynn) --00000000000082fbf605ccf8f188 Content-Type: text/plain; charset="UTF-8" On Mon, Sep 27, 2021 at 11:27 AM Konrad Baumgart wrote: > I was recently developing with js/ts and I liked the ease of returning > multiple items from a function as an object, while still preserving > their name. > I recently encountered this as well in Typescript. I believe the syntax is something like this? ```js const someProperty = "something"; const foo = { someProperty, anotherProperty: "Something else", }; // same as const foo = { someProperty: someProperty, anotherProperty: "Something else", }; ``` Obviously this won't work with PHP arrays, which is probably why you decided for `=> $someProperty`. I'm a big no-no when it comes to `compact()` and `extract()`. These functions make my life hell. Despite knowing what they do, it still takes me a lot of reading to try and figure out what they do. Even now I will have to go to the documentation to understand exactly how either of them work. Renaming a variable in a certain scope should not break something outside of that scope, and I see this as a big problem in the proposal. When I take your example, spot the error that will cause everything to break runtime with limited static analysis to let you know (hint: missing a g in the first variable). ```php return [ => $dailyAgregations, => $weeklyAggregations]; ``` Refactoring tools could just ignore the outside scope and automatically change it, yet this won't solve anything for text find and replace. ```php return [ 'dailyAggregations' => $theNewVariableName, => $weeklyAggregations, ]; ``` If something like this were to be introduced, I'd happily see it happen with anonymous objects and interfaces accepting properties: ```php interface DashboardAggregations { public readonly array $dailyAggregations; public readonly array $weeklyAggregations; } function getDashboardData() : DashboardAggregations { $dailyAggregations = []; $weeklyAggregations = []; // ... return { $dailyAggregations, $weeklyAggregations, }; // same as return { dailyAggregations: $dailyAggregations, weeklyAggregations: $weeklyAggregations, }; // same as (if properties in interfaces were allowed) return new class($dailyAggregations, $weeklyAggregations) implements DashboardAggregations { public function __construct( public readonly array $dailyAggregations, public readonly array $weeklyAggregations, ) {} }; } ``` If you then really need the destructuring, you could do something like this (just a random syntax chosen), though I'd still be in favor of leaving this aside as you now have a nice object with properties that are accessible. ```php {$dailyAggregations, $weeklyAggregations} = getDashboardData(); ``` This way php could verify the return value with the return type, and static analyzers can determine the structure of what's to be returned. Imo this functionality is not worth the headache with arrays. That said, it feels like this proposal is more of a tuple/multiple return. --00000000000082fbf605ccf8f188--