Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:112378 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 30318 invoked from network); 2 Dec 2020 17:57:06 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 2 Dec 2020 17:57:06 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 0935018053A for ; Wed, 2 Dec 2020 09:24:50 -0800 (PST) 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_20,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-Virus: No X-Envelope-From: Received: from mail-lf1-f46.google.com (mail-lf1-f46.google.com [209.85.167.46]) (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 ; Wed, 2 Dec 2020 09:24:49 -0800 (PST) Received: by mail-lf1-f46.google.com with SMTP id d20so5945423lfe.11 for ; Wed, 02 Dec 2020 09:24:49 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:from:date:message-id:subject:to; bh=FHfFYyDKWQ5C3NPK7OG/BL2se2Hr85RlDmbQYsoMOhY=; b=c34bz2qCK/6ZXmT50rq5t3iPEYPjiev8YQlOXTDP8XUP7BvpSRTjEcjbb/orIye+RB iZXE3IWkrh+mQSmhJs//ldHXQhjZwHHRCIb+ulHFR9Ggn29YrUafMTnp+7eEAA29fhg9 /3CbsWeioQcg7dDx5QiazaY6pMsvGn7S2f9aodvUVet5RfKRtXoeMM7akjO2SS8q0/vs ox1RmuTgKLJ0tI9Ph3JzTbj4yh334FJ4RvdKLaIghXVHi04M+bb9OfvYQtpGqeyh1Lwz TlxO8r06grZs7SwDrKnIpWhRQ0VAnI8mUeBkBjDpQA8q8bY9+fUffDWXDwyH9UCMy1pA Q7LQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:from:date:message-id:subject:to; bh=FHfFYyDKWQ5C3NPK7OG/BL2se2Hr85RlDmbQYsoMOhY=; b=rPs/63K6xuT44gIiiATSmPdFCef2hJWBWazQZaB8bTDTRyqUJL7bju8/eDEaAEM0Io rcv6wGQfmIlQwM847CseSmgXDlEiKnumZ0M+NBrceGU0xnm27WUGNMXVcehE2TTEYn4s uG8tP6ju0WaUj1HoquUkc4ioa0q+5RFL9WMcxjWrrvQr4C5c7e4GJLQglEUUVR1GtAM0 uYOfrLjqyozKVjukGfl+rSxVPyZjUP2Zz0ip+vpIueONdQlRBq+QfVx3DGL+u21norB+ d/xc4dqZ+em6Hdow4TPr43TGqfv6XH2Fy6DGovoiQzsEp+tjJPD24xgGVw3RxaRqeF9K IKdg== X-Gm-Message-State: AOAM530SosWclO03m/VBjeGi5ZzbwkS8I0+yp9Y/ABXhPwRm/zNepJ9Q 0qxq/FPtu7AZyg8hwONq0ftzxvHmJ/J1xIc4l0fl2c/8tjoeGA== X-Google-Smtp-Source: ABdhPJySyFDgvOQYl7joKMoqVQbvrB7pJ224FFmOEWpzC6nRRUW4+hPPrlyu/Js0err/l3wpV4nEUbqBijnENvUomZQ= X-Received: by 2002:a19:7c9:: with SMTP id 192mr1595326lfh.424.1606929885153; Wed, 02 Dec 2020 09:24:45 -0800 (PST) MIME-Version: 1.0 Date: Wed, 2 Dec 2020 17:24:33 +0000 Message-ID: To: internals@lists.php.net Content-Type: multipart/alternative; boundary="000000000000fbce0f05b57e849d" Subject: Suggestion: Inconsistency: Allow array spread operator to work on string keys From: florian.stascheck@gmail.com (Florian Stascheck) --000000000000fbce0f05b57e849d Content-Type: text/plain; charset="UTF-8" Hello! With PHP8 released and the named arguments RFC being implemented, there's now an inconsistency in how the spread operator works. Historically, the spread operator was first used in PHP 5.6 for arguments: function php56($a, $b) { return $a + $b; } $test = [1, 2]; php56(...$test) === 3; Then, with PHP 7.4, the spread operator was also introduced for array literal syntax: $parts = ['apple', 'banana']; $fruits = ['strawberry', ...$parts, 'grape']; $fruits == ['strawberry', 'apple', 'banana', 'grape']; The RFC back then explicitly excluded [1] string keys, to make the functioning of the spread operator consistent with how it's used for function arguments. Now, in PHP8 we can do: function php8(int $a, int $b): int { return $a + $b; } $test = ['b' => 40, 'a' => 2]; php8(...$test) === 42; However, string keys in array literals are still not possible. So the limitation that once was introduced for consistency now led to an inconsistency. I suggest to allow string keys to also be used in array literals: $template = ['created_at' => time(), 'is_admin' => 1]; $db_rows = [ ['name' => 'Alice', 'email' => 'alice@example.org', ...$template], ['name' => 'Bob', 'email' => 'bob@example.org', ...$template], ]; What's your feedback on that? Do you see any obvious problems with that approach? I searched through the archives and couldn't find this problem being mentioned before. -Florian [1]: https://wiki.php.net/rfc/spread_operator_for_array#string_keys --000000000000fbce0f05b57e849d--