Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:119485 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 37708 invoked from network); 8 Feb 2023 14:40:45 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 8 Feb 2023 14:40:45 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 81B59180511 for ; Wed, 8 Feb 2023 06:40:44 -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=-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, T_SCC_BODY_TEXT_LINE 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-wm1-f43.google.com (mail-wm1-f43.google.com [209.85.128.43]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange ECDHE (P-256) server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by php-smtp4.php.net (Postfix) with ESMTPS for ; Wed, 8 Feb 2023 06:40:43 -0800 (PST) Received: by mail-wm1-f43.google.com with SMTP id f47-20020a05600c492f00b003dc584a7b7eso1616501wmp.3 for ; Wed, 08 Feb 2023 06:40:43 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=to:subject:message-id:date:from:in-reply-to:references:mime-version :from:to:cc:subject:date:message-id:reply-to; bh=vpmg7rum/A2qWzjDJ2+hN3krBDWy9vTIgEhEbDVEn8c=; b=bM2DynROekpKpUa7czPv2zg7HYpBiaqqbgHZWaHA+Mrl2QgmvuhoLpDsHiRTn6iJXw btvcbFjjPR8f988UPjTs/MKGFP6lLf8QZF5h2wuA+LAsAz9oW+KlPvqeIMOyhbywaSRw kC5robOmc9ckwrjdZD8VsOexmHy4oAyARht9+3EjrCjTaUMCAnlyewPmkYIcnD3HB9hW wr/NvelNyxzJlR1lyObQ48q4iUhBUWiCBZg7bloDxYOlm7Zzsr20g4EUoTRx1op/lw0o jhG53kHrPdbvWq9ZLOxLopl6pbdh9EzJxXRfaUKE/Fv2vQKG6VEGiB48a+lkcTsmLOkm JQLg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=to:subject:message-id:date:from:in-reply-to:references:mime-version :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=vpmg7rum/A2qWzjDJ2+hN3krBDWy9vTIgEhEbDVEn8c=; b=Y/XTejBAqOTkv3PDWl7KB1TxrwOO+xa9uZDU1g+nXmV4oU3wOs4Qa5ZWnbSAHHBFcw lhAf9EyAdZzRSkHWZ4qcGnM0LVBDjmbHO1tG7ethhPAegvZuzg3yVLhOOjQCl86ASz69 lcSSOz+JVSwaHj14g2XGPvFTwY3cORm46/DuT87W2l0aJSfBOuyV/W6ftPGNV44nG+Qw 6y6PyNQoqvDO4Z5JlfYuJ0mjAfUWnHMPc6lB3Z343thI5r5u9QEhvnX3vWIfOZnvixfW B6cOatkGHIRQELkq3DIGjfl9MH2dE1JwR/zvZQdnW2L9AZibPJGwtSMJtD4R6BWjGXBI qKEw== X-Gm-Message-State: AO0yUKWmD0E/BvbqCdNFlNI72Zc2UJBUFUq77kavhl1eAufg2MfVgzCC CIU65JO0qZaF1Sdcma210aDyTnDxqi166eGJwWVUwU4c4fI= X-Google-Smtp-Source: AK7set/DNTI8BzQeCoWf2cRBZ7m2u1uZ2L8fO4tK3VmG+bGknNunQpcl8Pv8OAfLzb7kU1u+SwlRA9WsXuJ5v+eJyd0= X-Received: by 2002:a05:600c:3b93:b0:3e0:16c:c0cc with SMTP id n19-20020a05600c3b9300b003e0016cc0ccmr196158wms.166.1675867242755; Wed, 08 Feb 2023 06:40:42 -0800 (PST) MIME-Version: 1.0 References: <18627e9c82d.1185ba82a2669697.6762525826203739336@wendelladriel.com> <8c3fb742-4367-0e4a-b96e-142657ac902c@gmail.com> <5edaa45e-08ff-9529-3a1b-42f655a68497@gmail.com> In-Reply-To: Date: Wed, 8 Feb 2023 14:40:30 +0000 Message-ID: To: PHP Internals Content-Type: multipart/alternative; boundary="000000000000b1fc4105f4313fc8" Subject: Re: [PHP-DEV] RFC Proposal - Types for Inline Variables From: rowan.collins@gmail.com (Rowan Tommins) --000000000000b1fc4105f4313fc8 Content-Type: text/plain; charset="UTF-8" On Wed, 8 Feb 2023 at 00:13, Sergii Shymko wrote: > I think, typed variables would be a necessary prerequisite for typed > arrays and eventually generics. > If my understanding is correct, the primary concern there is performance > overhead of iterating items for type checking. > With typed variables, type checking is done at assignment and passing > typed array around becomes lightweight. > It would be enough to type check against a known array item type without > iterating over items. > > For example: > function do_something(array $strings) { > ... > } > array $names = ['John', 'Jane', 'Jake']; // items are type > checked similar to variadic argument unpacking > sort($names); > do_something($names); // lightweight type check as for scalar types This is probably more complicated to implement than you're imagining. Firstly, there is a key distinction between *variables* and *values*: when you say do_something($names), the receiving code in do_something doesn't know that the argument it was given is the variable $names, with type array; it just receives the value ['John', 'Jane', 'Jake']. That leads to another distinction: the variable has a single type *constraint* ("must always point to a value matching this type"), but the value has multiple pieces of type *information* ("has been checked as meeting these different constraints"). Both can also be deduced using the relationships between types. For example: function do_something(array $strings) { ... } function filter_non_strings(array $items): array { ... } array $mixed_list = ['John', 42, 'Jane']; $mixed_list = filter_non_strings($mixed_list); // when calling filter_non_strings, we know that the value matches array, so can deduce that it matches array as well // when assigning the result back to $mixed_list, we know (from the return type check) that it matches array, so can deduce that it matches array as well do_something($mixed_list); // since the value hasn't changed since it was returned, it still matches array, even though the variable doesn't have that constraint sort($mixed_list); // for this call to succeed, we need to know that the variable is an array; we can deduce that from knowing it matches array or knowing that it matches array // as a minimum, we need to assert that after modifying it by reference, it still matches array, as required by $mixed_list // but do we also still know that it matches array? It's all probably doable, but I think it's the other way around from your initial statement: working out how to cache type checks would be a pre-requisite for implementing local variable types. Regards, -- Rowan Tommins [IMSoP] --000000000000b1fc4105f4313fc8--