Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:123733 X-Original-To: internals@lists.php.net Delivered-To: internals@lists.php.net Received: from php-smtp4.php.net (php-smtp4.php.net [45.112.84.5]) by qa.php.net (Postfix) with ESMTPS id 4ADC21A009C for ; Fri, 21 Jun 2024 15:43:38 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=php.net; s=mail; t=1718984692; bh=gIDei7LHL3WAKMvQSsoRzImuXhZ0DDuIUssmRzjTH0c=; h=Date:Subject:To:References:From:In-Reply-To:From; b=LJ0KfUTW9RXrZwiGc/F0JLi4Ldps00s+UlD8WhLs0u0UKAF2Hxf0r/WZGewaLWOsM DhAMHNZ1GdOzqgKX4VXHDNUQhW9SxzwK9zEf+BQuIA8ay2OFwSFziHgNZZx8lY0oTI 473QvoGNtxEeVDkzhkIAP54WpCxZRshdOaDjaPOtiNoLbhf/UvN76oNtN8Dimi+MTK 6sWrPcyfDWN1iCMuKABT/XIMKG66oXGjuIzud6Azm9XIC6TGQOhJgljcE99WtlRptX Cm1c0xt+w3FL+btFRfa5DDV/A1WCnQ6v9ybwIIwQ7Fu+/sVxtaMeDHCHWqYnOQOkVJ DaBatlD3h7Www== Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 5B6D118005C for ; Fri, 21 Jun 2024 15:44:51 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 4.0.0 (2022-12-13) on php-smtp4.php.net X-Spam-Level: X-Spam-Status: No, score=0.6 required=5.0 tests=BAYES_50,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,DMARC_PASS,FREEMAIL_FROM, RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H2,SPF_HELO_NONE,SPF_PASS, T_SCC_BODY_TEXT_LINE autolearn=no autolearn_force=no version=4.0.0 X-Spam-Virus: Error (Cannot connect to unix socket '/var/run/clamav/clamd.ctl': connect: Connection refused) 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 X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by php-smtp4.php.net (Postfix) with ESMTPS for ; Fri, 21 Jun 2024 15:44:50 +0000 (UTC) Received: by mail-lf1-f46.google.com with SMTP id 2adb3069b0e04-52cc148159dso2379458e87.0 for ; Fri, 21 Jun 2024 08:43:36 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1718984615; x=1719589415; darn=lists.php.net; h=content-transfer-encoding:in-reply-to:from:content-language :references:to:subject:user-agent:mime-version:date:message-id:from :to:cc:subject:date:message-id:reply-to; bh=5pFWZBBn1R1rdMeGJUP8V0AG9+dfCBP9C4pDp7U+Yfo=; b=m+REa4ELfEYWu70nymB/OQS/UnCeCfAh7/LvDjAbmhGgSS6KKpdHLYJdtVKjk/8ksL /5PrK0nalSBwSduj3yO5mNgFRkyF63J4unSqI2TFFcYMR1hAPuk3QFVcDIQDsrHBkKfk se+3WikY014FtxlIneuFsPVY5bb3hi8ud9uW4smsrUIL8rjB/4IJjh4AT6MGLWWH6OsK ADCdI+9ia/VMERlB2TdzVBsEW/FCRruryKaAf2zh5IDouTVU2195ZjuQQ8vdPxKWgvAs JMd15hPMAx5FB6fyZdT51gtNUSlBVUxrl/OphoKYq6Fl51/OolUVUtdbwAVBHURj0ndZ JnwQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1718984615; x=1719589415; h=content-transfer-encoding:in-reply-to:from:content-language :references:to:subject:user-agent:mime-version:date:message-id :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=5pFWZBBn1R1rdMeGJUP8V0AG9+dfCBP9C4pDp7U+Yfo=; b=L7ccCrRZ3onLne46aQ/ngjvddrqCAH7lXd03+iWnfKo9otfsxpoWlmd+FM+VRsxcQT ghx8fvfjvJvpXCPpkKf4nHUj1QieMys3yaKpwwNPF7cYS72gVNqhdHRNHncGwIezuMI+ gI9+ah7gEXPYk580TtbSOJ2o6SOuoj4qCosW4jXD/41zVEZyD2qTqrjJfm6W+grMmQud q36Tz58CONxCI1H+n9BHVsymeHXcwt5sldaUjll8+tQAOkstPEopOIugqu9zeHgOSK0V yd1yZK3zp2A1AGyUMIzFfEoMYt2DKmFi7KJKttHchwO5V8sbmo6cHBIgI2YXoqm86BC9 mFNg== X-Gm-Message-State: AOJu0YzAIDwHFRD55AfSgYIrAkCwuLg2abSjZUQSN7vYx65fd/8HS3XS XnoDHr3DO+vfGgLPjCobs2LYUfaK8DdzAyuUkRWzjWMoc5RvIeDOkNuEZw== X-Google-Smtp-Source: AGHT+IFaheUij6LIDfeQ3kL0BKi99c4jAd1LEoQIDfXVCsrLjkKuHlsg+XOjVMsvGb14Y3ncQMosog== X-Received: by 2002:a05:6512:684:b0:52c:5925:fc9d with SMTP id 2adb3069b0e04-52ccaa887a7mr6136044e87.45.1718984614577; Fri, 21 Jun 2024 08:43:34 -0700 (PDT) Received: from [192.168.0.103] (178-117-134-240.access.telenet.be. [178.117.134.240]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-4247d212254sm69574315e9.45.2024.06.21.08.43.34 for (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Fri, 21 Jun 2024 08:43:34 -0700 (PDT) Message-ID: <7d825b1d-e584-4916-9435-3561b9c54c26@gmail.com> Date: Fri, 21 Jun 2024 17:42:11 +0200 Precedence: bulk list-help: list-post: List-Id: internals.lists.php.net MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [PHP-DEV] [Early Feedback] Pattern matching To: internals@lists.php.net References: <2a6b92eb-d5e9-4a1a-9548-a068ac42ebd2@app.fastmail.com> <02ee8831-43a0-4857-886e-7f54fb42a99d@varteg.nz> Content-Language: en-US In-Reply-To: Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit From: dossche.niels@gmail.com (Niels Dossche) On 21/06/2024 14:43, Robert Landers wrote: > On Fri, Jun 21, 2024 at 5:08 AM Andreas Hennings wrote: >> >>> Ilija and I have been working on and off on an RFC for pattern matching since the early work on Enumerations. >> >> I like what I see, a lot! >> One quick thought that came to my mind, regarding objects: >> Could we check method return values? >> >> if ($x is Countable { count(): 0 }) ... >> if ($p is Point { getX(): 3 }) ... >> if ($x is Stringable { __toString(): 'hello' }|'hello') ... >> while ($it is Iterator { valid(): true, current(): $value, next(): null }) ... >> >> Maybe it goes too far. >> >> For the variable binding, I noticed that we can overwrite the original variable: >> $x is SomethingWrapper { something: $x } >> In this case the bool return is not really needed. >> For now this usage looks a bit unintuitive to me, but I might change >> my mind and grow to like it, not sure. >> >> >> For "weak mode" ~int, and also some other concepts, I notice that this >> RFC is ahead of the type system. >> >> E.g. should something like array be added to the type system in >> the future, or do we leave the type system behind, and rely on the new >> "guards"? >> public array $values is array >> OR >> public array $values >> >> The concern here would be if in the future we plan to extend the type >> system in a way that is inconsistent or incompatible with the pattern >> matching system. >> >> --- Andreas > > I'm always surprised why arrays can't keep track of their internal > types. Every time an item is added to the map, just chuck in the type > and a count, then if it is removed, decrement the counter, and if > zero, remove the type. Thus checking if an array is `array` > should be a near O(1) operation. Memory usage might be an issue (a > couple bytes per type in the array), but not terrible.... but then > again, I've been digging into the type system quite a bit over the > last few months. And every time a modification happens, directly or indirectly, you'll have to modify the counts too. Given how much arrays / hash tables are used within the PHP codebase, this will eventually add up to a lot of overhead. A lot of internal functions that work with arrays will need to be audited and updated too. Lots of potential for introducing bugs. It's (unfortunately) not a matter of "just" adding some counts.