Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:109684 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 17839 invoked from network); 16 Apr 2020 14:30:47 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 16 Apr 2020 14:30:47 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 7B76A1804DD for ; Thu, 16 Apr 2020 06:00:55 -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, 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-lf1-f49.google.com (mail-lf1-f49.google.com [209.85.167.49]) (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 ; Thu, 16 Apr 2020 06:00:54 -0700 (PDT) Received: by mail-lf1-f49.google.com with SMTP id j14so5501963lfg.9 for ; Thu, 16 Apr 2020 06:00:54 -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 :cc; bh=x2/HEpNUfD3pJHJ+JC7IsTTrlq8yZg6rdPfA/0WiBSI=; b=le6OdQnkpVFBkCj0Smi4Xdo4fmfGD5yocARObAenE0PQCn3YuPkL4ClcPGwjeJNSe0 1g82yQhDAqAl4/8BkOzFrJZ7oOl1jvOt6xCuuOqeamySjOduwnq0JnISgW+a6sST8G1G Vybe6v9OoedszQ5Z//z6ZUI18PuZsilx62LwKsvlJXO3WO5NwHNaVlkwDTscZlYub/Wk pc5taz+myoEOIA8dTuA6zS/OuCPAQsuoY1IAT9rjXdpgMUhVpB3R5A1ZjiUMeY/kBKS6 T6BydYWBY9MhLqFeuiHPm1vCehqhdZW6E/WwQO2bRu235Oscp3Q08gPmjsSGvsXq4jHw 2FdA== 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:cc; bh=x2/HEpNUfD3pJHJ+JC7IsTTrlq8yZg6rdPfA/0WiBSI=; b=sV3e48wEyHJ9wnuvi/Y1WiJeTgiRqa/33KHoUtDYsre0LsfD9lN+0ROFfdkKsFYH/k jzSTglIOoz+Fk4qiS8PMyXRtihWCZDQHm+B5z5a95wcjHvEN53yO/zDhw1PwPoxJdIV1 lTS45MdLxYp1pHpYO3u4PRTsA/wS55LJYKWtRERuDvKafcoazsLiq6PY9okztNrVuPIw AwwxJRxU8Uf9cokE4xS5qm+Xl8V9GjoayfwdzJs2vMzYxCg6oQ/J+g0uhy4fRi7kScd4 2j4yDn1be0ajc7vmJPEJCZA+9Q6BOO6mmhNHNr1hjGHLV0NrB5VnzQvwjjnlPeo4/6xr PEGg== X-Gm-Message-State: AGi0Pub+rDcLJiqny3ISwJ82XEce30boyZVTsBAqdY5/wuTjFOQpWAMs JsGa/7+y2h8Shi8R/tb3lznfYZs2DRMmjsia0g== X-Google-Smtp-Source: APiQypLiQET57efyAZbG0ZUI1ms1+WMwTzGrG3mhj3WI7YYGwFVBola3//UVwKGnFH9wxk42sVW3k7oM1XOQRoIZHhc= X-Received: by 2002:a05:6512:1095:: with SMTP id j21mr6059288lfg.118.1587042052479; Thu, 16 Apr 2020 06:00:52 -0700 (PDT) MIME-Version: 1.0 References: <2b8e5da5-2f21-57a2-7e7d-243c686cc69d@web.de> In-Reply-To: <2b8e5da5-2f21-57a2-7e7d-243c686cc69d@web.de> Date: Thu, 16 Apr 2020 15:00:40 +0200 Message-ID: To: Enno Woortmann Cc: internals@lists.php.net Content-Type: text/plain; charset="UTF-8" Subject: Re: [PHP-DEV] Type hints in array destructuring expressions From: guilliam.xavier@gmail.com (Guilliam Xavier) On Thu, Apr 16, 2020 at 12:20 PM Enno Woortmann wrote: > > Hi together, > > as the voting for the "Type casting in array destructuring expressions" > shows a clear direction to be declined (sad faces on my side, I really > would've liked it as a feature completion of the casting feature set > without the need for a really new syntax, as the parser also already > coverred it, but ok, time to continue :D ), combined with a strong > interest for the topic "type checks in array destructuring expressions" > (as well in the discussion as in the vote) I've set up a first draft of > an RFC covering this topic: > > https://wiki.php.net/rfc/typehint_array_desctructuring > > The discussion around the casting in array destructuring brought up the > idea to perform regular type checks in array destructuring expressions. > This RFC proposes a new syntax to type hint a variable inside an array > destructuring expression: > > $data = [42, 'Example', 2002]; > [int $id, string $data, int $year] = $data; > > The type hints behave identically to the type hints used in method > signatures (compare https://wiki.php.net/rfc/scalar_type_hints_v5). This > especially includes a behaviour depending on the strict_types directive. > > Additionally to scalar type hints also object type hints are possible. > > I've not yet started an implementation draft but I think this one will > be more tricky than the type cast implementation approach. As the parser > re-uses the array definition for array destructuring expressions (which > also leads to the type cast implementation without touching the parser) > adding type hints to the definition would also affect other parts of the > language eg. something like: > > $x = [int $a, string $b]; > > would be parsed by the parser. I'm currently not able to grasp the > impact of such a change. But that's another topic. > > Let's focus on the idea itself instead of the implementation first. > Thoughts on the first draft? > > Cheers, Enno > > -- > PHP Internals - PHP Runtime Development Mailing List > To unsubscribe, visit: http://www.php.net/unsub.php > Hi, thanks for proposing, I guess it could be useful (or convenient at least). Just 2 thoughts: - It has been requested several times on this list to stop using the wording "type hint" and instead write "type declaration" ;) - When I see this example in the RFC: ``` $years = [["now", 2020], ["future", 2021]]; foreach ($years as [string $description, int $year]) { ``` I immediately think of a similar: ``` $years = ["now" => 2020, "future" => 2021]; foreach ($years as string $description => int $year) { ``` or: ``` $foos = [new Foo("one"), new Foo("two")]; foreach ($foos as Foo $foo) { ``` Now I remember this has already been requested (e.g. https://externals.io/message/104485#104488), but that's not really the same use case, so I don't know if it could/should be included... Regards, -- Guilliam Xavier