Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:121801 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 54794 invoked from network); 24 Nov 2023 10:50:02 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 24 Nov 2023 10:50:02 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id C671E180032 for ; Fri, 24 Nov 2023 02:50:06 -0800 (PST) X-Spam-Checker-Version: SpamAssassin 4.0.0 (2022-12-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,DMARC_PASS,FREEMAIL_FROM, RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H3,RCVD_IN_MSPIKE_WL,SPF_HELO_NONE, SPF_PASS autolearn=no autolearn_force=no version=4.0.0 X-Spam-Virus: No X-Envelope-From: Received: from mail-oa1-f52.google.com (mail-oa1-f52.google.com [209.85.160.52]) (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, 24 Nov 2023 02:50:06 -0800 (PST) Received: by mail-oa1-f52.google.com with SMTP id 586e51a60fabf-1fa0885e1c2so59859fac.0 for ; Fri, 24 Nov 2023 02:50:01 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1700823001; x=1701427801; 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=j8Dl+ndhjJrwHuHXgcHdLRd4kaHbYyezSbrh0oUcRYE=; b=G9lV+p41h6jf4bRSVlu75rySdi4gDLqwArn25m9uQGtM048riCk2F5Cd0VX7/ClBn+ 4+hQIhkAFtjs012Phe4NHYVCAYGdirlCzPgtP32dd5GG8UxWUfJGfQv+UB8zHTHT/fqJ Fpo6A8DLMnl3T+i15A7YLv2yX2WJ5RwVNuvFDYOJMYAjD0q+PWRfNG2CZDlOVZp2oHuH 1vmJjcvA8l6s15pS1/jMM2KY96F7eIOF4ebJk8sTSM+kKMRpxD49U35K3JMlq7jc0QTr AuOoQPhkgFR7yC6t+QO87scDxMusPhnbMnU2aHKxz9W7hC5mfVjsSriJG0s3PNqz2pmJ dtQg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1700823001; x=1701427801; 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=j8Dl+ndhjJrwHuHXgcHdLRd4kaHbYyezSbrh0oUcRYE=; b=Li5QS5TI8Z1WqW/9VN1Vs61scjEm8DIbf8EUAKLZsmJV3TJ+Dmcnnwl82Vvxav1qR1 7HC2eY+Of5VsLSznxyC2t0R3ChHYek6x71ZBq/7kmInWLzLuE9lAzDO8SjRO9vcgoU5I nMU4jEUYCzKquIS2rSeOoLjiwa04BJbGT0QZl6YNC5nQ7tOrzR0XL5Px8yNgEsbRzavw pTx0EQ6YaOAIiEd6dY4JJXt01cHcJY98RQT4cp2yPFS5w+vkSzW4yOoVuqKQu6uEcteH xhswcFbkUW0c68BSvUiTiuLCjE6u2egOil95qJXrQ9csVxOx1CjiMnwFj7AYE3uSQJpl Z+EQ== X-Gm-Message-State: AOJu0YwefJEUVHHHfOT9nC5qku/B6rrbyxbkRALDD0AWoUDY11D45Eo4 LQSNaPIDJTdzcPnFL9nHedQasP4iVEc/NBnxn4mcLOcPuiM= X-Google-Smtp-Source: AGHT+IH6nZWCLYjvAn0uQd8PXyCbT4w1oS85uDslZw9Lgi6GxK00rgrImqqFD08MxbUSuO5XvSSvzYDCOUvyUC7vJKw= X-Received: by 2002:a05:6870:ab18:b0:1ea:bb5:a531 with SMTP id gu24-20020a056870ab1800b001ea0bb5a531mr3108532oab.13.1700823000719; Fri, 24 Nov 2023 02:50:00 -0800 (PST) MIME-Version: 1.0 References: In-Reply-To: Date: Fri, 24 Nov 2023 11:49:49 +0100 Message-ID: To: =?UTF-8?Q?Micha=C5=82_Marcin_Brzuchalski?= Cc: chopins xiao , "internals@lists.php.net" Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable Subject: Re: [PHP-DEV] Is it possible to add asynchronous loop call function ? From: landers.robert@gmail.com (Robert Landers) On Fri, Nov 24, 2023 at 11:27=E2=80=AFAM Micha=C5=82 Marcin Brzuchalski wrote: > > Hi Robert, > > pt., 24 lis 2023 o 10:24 Robert Landers napisa= =C5=82(a): >> >> ... >> You can also emulate this with: >> >> class Defer { >> private function __construct(private \Closure $callback) {} >> public function __destruct() { $this->callback(); } >> public static function _(\Closure $callback) { return new self($callba= ck); } >> } >> >> and use it like: >> >> function writeSomeStuff() { >> // open files >> $deferred =3D Defer::_($closeFiles(...)); >> // do stuff >> } >> >> So long as a reference exists to $deferred variable, the deferred >> method won't be run. If the variable is local to the method/function >> being run it, the deconstructor will be called after the function >> returns. >> >> It isn't the most beautiful thing in the world, and easy to forget to >> store the result of Defer, but it is handy sometimes. > > > This is interesting which makes me thinking if forget to store it could b= e prevented. > I think requiring a ref could help with that: > > class Defer { > private function __construct(private \Closure $callback) {} > public function __destruct() { ($this->callback)(); } > public static function _(\Closure $callback, &$var) { $var =3D new self= ($callback); } > } > > $deferred =3D Defer::_($closeFiles(...), $foo); > > Without $foo there'd be an ArgumentCountError. > > Cheers, > Micha=C5=82 Marcin Brzuchalski Hey Michael, Thinking about it some more, I think we can go even further than that and actually implement defer (enjoying the code golf)! function defer(mixed &$anchor, \Closure $deferred): void { if(is_resource($anchor)) { $anchor =3D new class($anchor) { public function __construct(public readonly mixed $resource) {} }; } static $mapping =3D new WeakMap(); $mapping[$anchor] =3D new class($deferred) { public function __construct(private \Closure $deferred) {} public function __destruct() { ($this->deferred)(); } }; } function writeFiles() { $handle =3D fopen("/tmp/scratch.php", "wb"); // todo: write data defer($handle, static function() use ($handle) { echo "\ndeferred\n"; fclose($handle); }); echo "\nafter defer()\n"; } writeFiles(); echo "\nafter writeFiles()\n"; or something like that.