Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:109689 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 37488 invoked from network); 16 Apr 2020 16:20:38 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 16 Apr 2020 16:20:38 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id E759A1804C2 for ; Thu, 16 Apr 2020 07:50:49 -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,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-lj1-f176.google.com (mail-lj1-f176.google.com [209.85.208.176]) (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 07:50:49 -0700 (PDT) Received: by mail-lj1-f176.google.com with SMTP id z26so8067435ljz.11 for ; Thu, 16 Apr 2020 07:50:49 -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=sXD3C1NdCSNtZ351yZH2oWQwJUE4ADrpfqvWxvwf2zA=; b=oknqzaj3tZeLK/wekYsd8hGobj1IHYCnii+doV0YxzlMuaoK1tkkVIk8oI9QrKmD7T bjIRkZaSDEERhBio2DjLckCRPEnlYrwrinZWKSXnv7cfk4oB/71WJAARUU2V8LG1GjnF lcgGUySRDmAu/TuiB/onIDzJm1w8fdkFdLHWzuKhSxmbPEy2Sli2yM17IzzfkKrfe6yz ecNM3rg9tc8B0RIpyE+fS22u2rnawrrQ3DVGZ5NnRJGSuNlIOZCX/5iw0IXQojRtXQwp s76FWGcPNQ1fJKNEasiJubv8LOPq3wb0X1z4xus3lBRqV2y1e03wtDSf55rnJpqor7CJ ti2w== 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=sXD3C1NdCSNtZ351yZH2oWQwJUE4ADrpfqvWxvwf2zA=; b=F3vvn+boJYrSZ7mW6CFzjzGAhF39uRc3PrRQmr9YuiYycCrXHT6hxQzUoVEjfEpQno dc/aeDcApPYp/5tpaRwUjXI73zpdJHDKVzaxecMGc57SOo9U/aETrDAqXFhduazRyqPz TqPRyXqWvfnIEmSUewj9ODZEClsZKAQTeujtLL6ku2g6q3waWyLBPlsmy81M407eovGt eZ2qjMEFN0u4vbAp2aZC01mH4lLm0URvkgVDQp5fnn034nomzoQUsEezrGU76UsURWJ4 vLg8mDYcX7oxoB30xsvua3bNjz3IVMaHIbdDBesmduvkM30jvRbYVoiuZnGqcRIZMTsr 3I4g== X-Gm-Message-State: AGi0PuaGQDtrf/6TLpRpN052EGOw2urTVZEQR8CgY+sr7MImH22w1XYY TTK1i9cDMUCzQFBJnQ9VGiQzXWBFC/33uDOIC8Y= X-Google-Smtp-Source: APiQypJ5yN69azO2oQ30nKt8qDFovm4cOCTLrDMuwelCp3sSzwwBCs4J52oyTJRTDvJgSEYRIvFS0A+NwUdmYG59UvA= X-Received: by 2002:a2e:9b4a:: with SMTP id o10mr6577262ljj.117.1587048645516; Thu, 16 Apr 2020 07:50:45 -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 16:50:29 +0200 Message-ID: To: Enno Woortmann Cc: PHP internals Content-Type: multipart/alternative; boundary="000000000000c1e23105a3698ecf" Subject: Re: [PHP-DEV] Type hints in array destructuring expressions From: nikita.ppv@gmail.com (Nikita Popov) --000000000000c1e23105a3698ecf Content-Type: text/plain; charset="UTF-8" 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? > As you say, this syntax will likely run into parsing issues. Once you take into account that types aren't just "int", but also "Foo|Bar", and also consider that we have support for references in unpacking, you could be left with something like [int|float &$foo] = $bar; Without actually testing, I would expect that this is going to run into issues of similar difficulty as the arrow function proposal encountered. Rather than integrating this into the destructuring assignment syntax, I think it would be more helpful to view this as the infallible variant of pattern matching, which is also being discussed in the "match expression" thread. As suggested there, the syntax could be something like let [int|float $foo] = $bar; and would throw if the RHS does not satisfy the pattern on the LHS (unlike the match variant, which is fallible). I think this might help to bring things under one umbrella, and to draw a clearer line between this functionality and typed variables (if it's just a pattern guard, then it's expected that the type restriction is checked instantaneous, not enforced persistently.) Nikita --000000000000c1e23105a3698ecf--