Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:123739 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 530D81A009C for ; Fri, 21 Jun 2024 17:20:04 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=php.net; s=mail; t=1718990478; bh=QsjO3Zp+s3n4bdSZpEkxrq48efoTfxUp/dWeXvU/EvI=; h=References:In-Reply-To:From:Date:Subject:To:Cc:From; b=Cs9TmXlAACPwN7LAK1eIlWrPGVvRWnzp8XUCB2Etrzpiza1SpGuAfC/2vpCTsrQHH PYoz1P4tiKTNN7DyF92XnHmmPYmBBC9hLJ2L/4vS1m4AjwUAOiHMu/72BIhTqS+u0B E+5orGDUSmF9Cq8uPIvhIko9lXJ2QwDqYkp6fzKi2Eq2u8kihtoPgR4HBiCBoTn3TX 1JU8umi6UDqhJTlKlIUSHDuYzijUb5RsFw6tJhiXIxSKBl1gBdq1GUDiS0k1aEZbu6 bofEdVyBDhBnYxa8z1hEVYwaU84mgz9KhlxgSXPJKjS6Y8sPOKLspS3xZPSp5/C3LB kYv3HN9UEo0Iw== Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id C7C0B1809B4 for ; Fri, 21 Jun 2024 17:21:17 +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-f53.google.com (mail-lf1-f53.google.com [209.85.167.53]) (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 17:21:17 +0000 (UTC) Received: by mail-lf1-f53.google.com with SMTP id 2adb3069b0e04-52c82101407so3915642e87.3 for ; Fri, 21 Jun 2024 10:20:02 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1718990401; x=1719595201; darn=lists.php.net; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=QsjO3Zp+s3n4bdSZpEkxrq48efoTfxUp/dWeXvU/EvI=; b=Vvg8t6eQUG6w4UK3f2v0EkOZ0tCV8XvR2BT1j3MoF/1Yxf7df3zbmB1hOSUb1OARAR DRhlkwYUfDgZNbSXmkSDkb0mpRfsDT2KVeeX+ycSgMvZ19fl60u9Abub7gWHGLp+u7B/ Z5CNDaig2UfwCJDf2ZZObnQ6YjXrUApvHutBjhlHQMj/lJ2UpSF8tVRpp2H5fGw1PH0g +Bfzgh7IwqvJ5vdRuwkyeuh7gHDxup8KGP+Lf2h3+J321U4eIdMVUUKK3FIA6xUkH0U+ hnIIt7g2H/UaOa6o6fnsUCgGPIem91x5amNFraLUYDiyizxVFcDPBaW3WE8nXq3020kF QSiw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1718990401; x=1719595201; h=content-transfer-encoding:cc: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=QsjO3Zp+s3n4bdSZpEkxrq48efoTfxUp/dWeXvU/EvI=; b=UETtZFfJju+voL4nmv0g+WKoyNy+gZlPEsn1gk8vUp+gs1iJDrTPRBZrw3tN1XEW6y S6pe3bUkw4h7oOwOfPsbgDsnfaRI6iux1kFVe5M9bpEloHWMhhwpVqBDEV6aSAxfrTus OeK0Gk9bRVGRZ8gNF4c6lbOCD1smGsITer8nMQvreGD+kIYObqnjzuwoj7fm88jysGAI /FIYy07L3ZHe+1e5QvQa/wjA6Ya6GsjgsOsw8wk2/WTmGGm4aR0VWlJ5hoZvIC4WJYQU s0ADYIfBRx/bjS17duHCZd08w/jdsvpA7ZuXtIPPy9IhA3Mp0OkDsbi+gX2yv/2TKn4Q EH4A== X-Gm-Message-State: AOJu0YyzRuGrL8Fop+jcHHZztQ+W9iH1Z+k/3Wo/sgFwlcYQv2Zpmu1p tFxny664MPXCDfoDbqyLZdTlwFrxZzwcnBGj2+I5GM4yMygatEazaLepMN/pW6K8NK1P9A7mm3N pe+HN9KScYNwwkCaNqwvBN+ySp3Y= X-Google-Smtp-Source: AGHT+IEku5Ufk4jNgxZlXQKMtVcnnNRswjqE7Z6kpFKhijVkA5HTA7lIXs+Ao2u3BMI1aWaiGrV+SC7L5YK1ud6Aclw= X-Received: by 2002:a05:6512:3994:b0:52c:cb8d:637d with SMTP id 2adb3069b0e04-52ccb8d6426mr8763742e87.5.1718990400714; Fri, 21 Jun 2024 10:20:00 -0700 (PDT) Precedence: bulk list-help: list-post: List-Id: internals.lists.php.net MIME-Version: 1.0 References: <2a6b92eb-d5e9-4a1a-9548-a068ac42ebd2@app.fastmail.com> <02ee8831-43a0-4857-886e-7f54fb42a99d@varteg.nz> <7d825b1d-e584-4916-9435-3561b9c54c26@gmail.com> In-Reply-To: <7d825b1d-e584-4916-9435-3561b9c54c26@gmail.com> Date: Fri, 21 Jun 2024 19:19:48 +0200 Message-ID: Subject: Re: [PHP-DEV] [Early Feedback] Pattern matching To: Niels Dossche Cc: internals@lists.php.net Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable From: landers.robert@gmail.com (Robert Landers) On Fri, Jun 21, 2024 at 6:58=E2=80=AFPM Niels Dossche wrote: > > On 21/06/2024 14:43, Robert Landers wrote: > > On Fri, Jun 21, 2024 at 5:08=E2=80=AFAM Andreas Hennings wrote: > >> > >>> Ilija and I have been working on and off on an RFC for pattern matchi= ng 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(): nul= l }) ... > >> > >> 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. Well, of course, nothing in software is "just" anything. As to how much overhead? I guess you could create a subtype of `array` that is typed, then people could use it when they need it and if it gets up-casted to an array, you can just toss out all the counts. As far as down-casting to the typed array, it would be no less inefficient than doing $arr =3D (fn(MyType ...$arr) =3D> $arr)(...$someArray); right now. Robert Landers Software Engineer Utrecht NL