Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:123726 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 8CBC51A009C for ; Fri, 21 Jun 2024 12:44:05 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=php.net; s=mail; t=1718973919; bh=j1GKoWahJtoLfFzu91bQuJ6URSy1KmuIjVFPCrU2+i4=; h=References:In-Reply-To:From:Date:Subject:To:Cc:From; b=dJQRNQIDEqVmPJEb8N+rwQerhMSRyFKlHGUOQRUaN/W+Fjf/R6q+pVQjZfVLlxJU5 4UrvKsqIi23N8VJ+8GpoJBfsPYP4x4qlDUJ+6MTiBXOYkKJ1bjafJjgnbYNAW1ihfX BAyk1AtJAGjrxNf0/us7WTnLWPVM2UiIq14KriIwov/6SrM2Bt+hqXDVInDLlVnADv nfFDhcsq9afFPwNGXBN84fjVKSFs2wd75TWZAQF/LBDi9Rp+PNHrXSOAbbHl4dtep5 PkDF6LgajIA+Itft8OBm6spr11XyHvpRAP/hBm6QWDrWtJuvABLRNuB5+gno/yvp/a HaoFtAJ92cw1g== Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 1AEA7180820 for ; Fri, 21 Jun 2024 12:45:19 +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-lj1-f182.google.com (mail-lj1-f182.google.com [209.85.208.182]) (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 12:45:18 +0000 (UTC) Received: by mail-lj1-f182.google.com with SMTP id 38308e7fff4ca-2eaea28868dso25933281fa.3 for ; Fri, 21 Jun 2024 05:44:04 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1718973843; x=1719578643; 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=j1GKoWahJtoLfFzu91bQuJ6URSy1KmuIjVFPCrU2+i4=; b=dJxFqTv9ioEARXFuRO2dH+Wan8xDrzdajeSS56hJ79c/EWJGkEuajBnULULn0y/2+o t40bNbRfHnz6mCUn1jKf6+6GQc1i0I6VXqyjVlGQrKFsDXWpQ5bDSDWylH/+Lwv3RgVY dax/vWFqgXttgWv45gdbuhOzTuOSWv3Bl3bjge6zlWm8QpkK0j5FYI7Oa2gzMAMUk9I0 ILZt0sUp/W7KUn2r+nx5DFSBN5k3jZYTqFxC+dWQ0S14VkFYCmF6mGwy3lTlwaIvKpvK LbZaWZ2Wg4B77qH3aR+9QS8+gxYGfBTIH/9ch/NOWGIqBlgc3+cL+N5C4FudAVInKtct AfIg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1718973843; x=1719578643; 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=j1GKoWahJtoLfFzu91bQuJ6URSy1KmuIjVFPCrU2+i4=; b=tkE3U/PrcMs4uWdUcAFemJcOudoBCuwqSEM8rELRB6Q1B1n+UM2cNzyoBVSrrdKhVI VtxgxNVa5qp2IuitEGWdHSEBj+TP/vGNok/NPp5G3JsQjAGkKpc8nwSYIFHrU5FeVWsr 3F8zuXbsSZBg2S8VDhZ+d6e1FhEA/q50U5yD1HXC5woX/0e6+Qp+eG7k1VxH6b+5wcLk gsFWdbgrA3oIGrFA4QZXp8PMz+7Qj4/wIgzg3RF4HStoa5PfktEKmQu6KEiOy4ly2eiM goTSn/dqqPD/0bNvzeUNB2Gjao0oJ31Em5qC1UGClUA5Fk77sMo5CDxFVO+lhERJzzLM IydA== X-Gm-Message-State: AOJu0YztEUp6jrGWmFe4uBt0JemU989m68z/krYJiFiZL1u40xFzGN0e VhZDlo2rx3fjJDZp/v0wDGbQaPctKjfFnRF8sp68nIZ+8Z7zefnO5unvp1DiDcv+tTMFC9NYoTH 3mcTnpgzWaM07dIw1RgpXYmye14ZzqoK3 X-Google-Smtp-Source: AGHT+IGiFcVKy5Hz5XrVaUjqr7yeRzq77U7n7d0/XXI16ZiHg8bWXUGfmrGKgIevc8MIR0nliMHG3nRfB7O91O4YSb4= X-Received: by 2002:a05:651c:1a0b:b0:2ec:4093:ec7 with SMTP id 38308e7fff4ca-2ec40931047mr69994351fa.30.1718973842417; Fri, 21 Jun 2024 05:44:02 -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> In-Reply-To: Date: Fri, 21 Jun 2024 14:43:50 +0200 Message-ID: Subject: Re: [PHP-DEV] [Early Feedback] Pattern matching To: Andreas Hennings 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 5:08=E2=80=AFAM 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 va= riable: > $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.