Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:127690 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 lists.php.net (Postfix) with ESMTPS id B4B001A00BC for ; Mon, 16 Jun 2025 18:11:16 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=php.net; s=mail; t=1750097357; bh=T3GK/JfEXUiI/1aVxb0xmjHjuEsBhDUydzzOBz3opqA=; h=Date:From:To:In-Reply-To:References:Subject:From; b=hHZzSjYao/sy+VZkClisx5BxkgrNXIqvqX3QtTbxHWJ5epE9wPGCk+2IqaJ1ybtX/ bJGg5YzVDqQU/4RVav/dHPGS02vlok6IxpiuL1aHbUgAO6ptQfb6cAx0DK1Xy30bFu ixQd86bYIXfuVP/TnPuoPFLCPBMBG0S0QuvhWmNpVBZ8RqpIq4isMiDDSKA9gMdrAz Fk9FbtyFyPKlUNdqu+6DmCMH5AMTc1/SGQvg2hmYfNsp0yOWgK0eH9O3njGqCiiAY7 5Ik2F+Km6XqIgepaMTXX8x1NSM3R6Rg+WABkTo0OxWdc87kYzkczFlqrQMgn9Q9Fo5 0TGV+u+YeV03g== Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id D32ED1801DC for ; Mon, 16 Jun 2025 18:09:16 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 4.0.1 (2024-03-25) on php-smtp4.php.net X-Spam-Level: X-Spam-Status: No, score=-2.8 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,DMARC_MISSING,RCVD_IN_DNSWL_LOW, SPF_HELO_PASS,SPF_NONE autolearn=no autolearn_force=no version=4.0.1 X-Spam-Virus: Error (Cannot connect to unix socket '/var/run/clamav/clamd.ctl': connect: Connection refused) X-Envelope-From: Received: from fout-a7-smtp.messagingengine.com (fout-a7-smtp.messagingengine.com [103.168.172.150]) (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 ; Mon, 16 Jun 2025 18:09:16 +0000 (UTC) Received: from phl-compute-10.internal (phl-compute-10.phl.internal [10.202.2.50]) by mailfout.phl.internal (Postfix) with ESMTP id D047413801B7 for ; Mon, 16 Jun 2025 14:11:14 -0400 (EDT) Received: from phl-imap-02 ([10.202.2.81]) by phl-compute-10.internal (MEProxy); Mon, 16 Jun 2025 14:11:14 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= garfieldtech.com; h=cc:content-transfer-encoding:content-type :content-type:date:date:from:from:in-reply-to:in-reply-to :message-id:mime-version:references:reply-to:subject:subject:to :to; s=fm1; t=1750097474; x=1750183874; bh=NhttiUzMeyrWQQXaNuCLf H3Bu5XuEKAP/PliARRTltA=; b=Pa/9kT0gLvc/jO6rvWYTVIIt4ABjhJt/kXiyj lMyq9iF51XqQmuO+suKLD2dxuon2HapCh+c6qZhcwxBC3nGOcybw9j6g1w3f1RXp gLFrgtS5iJLmwIdHyxqBzQm0IFLvlyu0Ozj9eEiqmuNMv3mmHhmIKt2rlIsP851y DAKKABiDq9Gg9yJ+CxYBRfHILWx1wK3r8rBUNJgQM/yy5HtfH/JFA+KPGJIcakFJ mylwhF0G9XrhbV4IcJYXo+m9WIYCEDipQVBeBlyBziFFasrcBLYsP6tsXxyqlAUW p4rHYeoMf1qyx0wH+dI4SnIJZV+ZvnKm7jJnHSkcgihneSGdg== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:content-transfer-encoding:content-type :content-type:date:date:feedback-id:feedback-id:from:from :in-reply-to:in-reply-to:message-id:mime-version:references :reply-to:subject:subject:to:to:x-me-proxy:x-me-sender :x-me-sender:x-sasl-enc; s=fm1; t=1750097474; x=1750183874; bh=N httiUzMeyrWQQXaNuCLfH3Bu5XuEKAP/PliARRTltA=; b=WHWNYprA4lZdKQez9 DmQG6RbwEB1M5FszsNUyfMapHVQRnUFjbDafaCIJTa/0ygZdQNbBIuJnlvkFWkBP Sc6taNQJSHtBtzNaiJeMprsdr9RqUCsTo5QXslRSXJNmAt1yDKg7uAVe61UZjqil h/znN51YYv3Y8aGyO3H5Kn1sIcDgHKvVPdY8gJwCHbl54XyTqFPfSels/DHohe8Z TZuPXdCnQike0Csflbm+/0a2p+y+wS8pQ+OxMiFT48Bh0CA1ZdRV6zDlPRFUB0ue vVKcA3S8r7XjEBNJ3VBCeGuTBEienv41lZAI2m7vS7/XQl4qJgxxGDZHUm41lY3i av4Zw== X-ME-Sender: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgeeffedrtddugddvjedvjecutefuodetggdotefrod ftvfcurfhrohhfihhlvgemucfhrghsthforghilhdpggftfghnshhusghstghrihgsvgdp uffrtefokffrpgfnqfghnecuuegrihhlohhuthemuceftddtnecusecvtfgvtghiphhivg hnthhsucdlqddutddtmdenucfjughrpefoggffhffvkfgjfhfutgfgsehtqhertdertdej necuhfhrohhmpedfnfgrrhhrhicuifgrrhhfihgvlhgufdcuoehlrghrrhihsehgrghrfh hivghlughtvggthhdrtghomheqnecuggftrfgrthhtvghrnhepffeiiedvhfdvgedutddt geetieeugeevhfetheeffeefteduiedthedtgeejueeinecuvehluhhsthgvrhfuihiivg eptdenucfrrghrrghmpehmrghilhhfrhhomheplhgrrhhrhiesghgrrhhfihgvlhguthgv tghhrdgtohhmpdhnsggprhgtphhtthhopedupdhmohguvgepshhmthhpohhuthdprhgtph htthhopehinhhtvghrnhgrlhhssehlihhsthhsrdhphhhprdhnvght X-ME-Proxy: Feedback-ID: i8414410d:Fastmail Received: by mailuser.phl.internal (Postfix, from userid 501) id 8DDA9700064; Mon, 16 Jun 2025 14:11:14 -0400 (EDT) X-Mailer: MessagingEngine.com Webmail Interface Precedence: bulk list-help: list-post: List-Id: internals.lists.php.net x-ms-reactions: disallow MIME-Version: 1.0 X-ThreadId: Td0f50d33a6b4a880 Date: Mon, 16 Jun 2025 13:10:49 -0500 To: "php internals" Message-ID: In-Reply-To: References: Subject: Re: [PHP-DEV] How hard would it be to add a "superyield" keyword? Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable From: larry@garfieldtech.com ("Larry Garfield") On Mon, Jun 16, 2025, at 10:18 AM, Olle H=C3=A4rstedt wrote: > Hello Internals, > > I was pondering a little about effect handlers today, and how they cou= ld > work as a replacement for dependency injection and mocking. Let me sho= w an > example: > > > require_once("vendor/autoload.php"); > > use Latitude\QueryBuilder\Engine\MySqlEngine; > use Latitude\QueryBuilder\QueryFactory; > use function Latitude\QueryBuilder\field; > > // Dummy db connection > class Db > { > public function getQueryBuilder() > { > return new QueryFactory(new MySqlEngine()); > } > } > > interface Effect {} > > class QueryEffect implements Effect > { > public $query; > > public function __construct($query) > { > $this->query =3D $query; > } > } > > class Plugin > { > /* The "normal" way to do testing, by injecting the db object. Not > needed here. > public function __construct(Db $db) > { > $this->db =3D $db; > } > */ > > public function populateCreditCardData(&$receipt) > { > foreach ($receipt['items'] as &$item) { > // 2 =3D credit card > if ($item['payment_type'] =3D=3D 2) { > $query =3D $this->db->getQueryBuilder() > ->select('card_product_name ') > ->from('card_transactions') > ->where(field('id')->eq($item['card_transaction_id= '])) > ->compile(); > > // Normal way: Call the injected dependency class dire= ctly. > //$result =3D $this->db->search($query->sql(), > $query->params()); > > // Generator way, push the side-effect up the stacktra= ce > using generators. > $result =3D yield new QueryEffect($query); > if ($result) { > $item['card_product_name'] =3D > $result[0]['card_product_name']; > } > } > } > } > } > > // Dummy receipt > $receipt =3D [ > 'items' =3D> [ > [ > 'payment_type' =3D> 2 > ] > ] > ]; > $p =3D new Plugin(); // Database is not injected > $gen =3D $p->populateCreditCardData($receipt); > foreach ($gen as $effect) { > // Call $db here instead of injecting it. > // But now I have to propagate the $gen logic all over the call st= ack, > with "yield from"? :( > // Effect handlers solve this by forcing an effect up in the stack > trace similar to exceptions. > > // Dummy db result > $rows =3D [ > [ > 'card_product_name' =3D> 'KLARNA', > ] > ]; > $gen->send($rows); > } > > // Receipt item now has card_product_name populated properly. > print_r($receipt); > > --- > > OK, so the problem with above code is that, in order for it to work, y= ou > have to add "yield from" from the top to the bottom of the call stack, > polluting the code-base similar to what happens with "async" in JavaSc= ript. > Also see the "Which color is your function" article [1]. > > For this design pattern to work seamlessly, there need to be a way to = yield > "all the way", so to speak, similar to what an exception does, and how > effect handlers work in OCaml [2]. > > The question is, would this be easy, hard, or very hard to add to the > current PHP source code? Is it conceptually too different from generat= ors? > Would it be easier to add a way to "jump back" from a catched exception > (kinda abusing the exception use-case, but that's how effect handlers = work, > more or less)? > > Thanks for reading :) > > Olle Algebraic effects is a... big and interesting topic. :-) If we were to = go that route, though, I would want to see something more formal than ju= st a "yield far." That's basically another kind of unchecked exception,= whereas I want us to move more toward checked exceptions. --Larry Garfield