Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:129702 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 EBC8B1A00BC for ; Sat, 27 Dec 2025 20:15:06 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=php.net; s=mail; t=1766866511; bh=G98JnNkDl8lsUY+DCSCpQmltSDhadzYshVGHWXaanoY=; h=References:In-Reply-To:From:Date:Subject:To:From; b=dgxH5MRrGH4hkSv7TilHImopMjep6wUrmmHbOfvLFzjTqG0gDVvHArW/5sgO7D8J2 EkR+lK98JQR2GahS1Jk38x80vtPUrGx/CmYlt1lECFmRU+d9JCmKQgeSET9cgp0dur 0XPZhNN/1T/TBYwgE5N/8zcvwYwAtAEBiFHpuIE06O2Q9GhOqke5ugXr8WeVdI63Eh 0sh1n5VFIKBlOcUkXuDQTmg/WBQ1pjg6PAqW2afVWQQ/kNeH0ip+/LYeN8vZx//8Sb Pb4jSAQpAa0jTqIgoS3GuCG16ZT6JB7BYMvfdEBhNU66iJk34WV+R1gGVfkCe7liPN 2fp3QUlf0xHzA== Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 9CA83180053 for ; Sat, 27 Dec 2025 20:15:10 +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=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 autolearn=no autolearn_force=no version=4.0.1 X-Spam-Virus: No X-Envelope-From: Received: from mail-qk1-f171.google.com (mail-qk1-f171.google.com [209.85.222.171]) (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 ; Sat, 27 Dec 2025 20:15:10 +0000 (UTC) Received: by mail-qk1-f171.google.com with SMTP id af79cd13be357-8c0f15e8247so843730385a.3 for ; Sat, 27 Dec 2025 12:15:05 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1766866504; x=1767471304; darn=lists.php.net; h=content-transfer-encoding:to:subject:message-id:date:from :in-reply-to:references:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=t56wQlWQe0JwEnwnIpIvASMWNsKLgNMy4zuAx5P6Q0Y=; b=Yeu7Oi7nDGu4yhalZgVS28biOzEjp49iVS4xducdOdSLJEqWXPMFfkb5g6V2AUAtXI JSqqr5joPujLp1gmOMPC6Ue/L/MjpWyzOiHGhDNo+mE3NzI1C0pzRIO3rSe1su6fIPBC vqMkZT7F0jNXFVjjC3zdvZyrgRn5z3Y0/5nrHOlDdcwTO1WXBc0QwU4ZQw0YhavfmdWS fs3+CMEZE+uSh0CowUs4H9hePWXNlY6IRupO1LsPdCyhWfB9eHCIhslzcjIZqznm60qW +oePKzWhawAbdgLnV3Yb6rzAjPOAWe0kjOPb9JnE0LO+8CvBhBdBMTgZsL6GdfIQHWp2 q8aA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1766866504; x=1767471304; h=content-transfer-encoding:to:subject:message-id:date:from :in-reply-to:references:mime-version:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to; bh=t56wQlWQe0JwEnwnIpIvASMWNsKLgNMy4zuAx5P6Q0Y=; b=Fh302pfHGqPjTbQbBcTKty8kPF4mDhr7VRcFHtxBgjFUQz4GBakEp4BIC6IPi/SJyF K67DUyB37Lk1cg/g6MCOcsKd2D2yUpwolG5VH0g3fFjF2quCH/c+7OmYJD8Ky+oc1v/C FlVfC2vTi9d5mCt5RJb2xhWR8Ur9jlVNBQfvXH477cwhFZ1NgiTnr7IwhTx0DKGcInG1 n5eM+xNfp7dIgg/4nzAepXbmkUV5fImXQvyBl49NCkkpf+Ewgi1h6rtNuFsOYzDw52Rn Ft18o8Y7/ShCLo0MY0YERL7cvXsY/44ZTx242TpsP2jHvUNxifiBoBWPXNWDTO8m3QgS 8tbQ== X-Gm-Message-State: AOJu0Yy5+ICeT3mkr9QGfiV/LzjxOWupY3vnbtyqCDIsyvsgCxgiRB5+ B2TpuHLo93qdC3atmHl+uZ3GBqf0jQwOleaiL4M20oQMgB56YhYDWX3EopVWVoryYsRlgXzMiUm WSQZE6AwRsvFbQG1Pf+ff2H5gz+9lHXc9dnTDhsY= X-Gm-Gg: AY/fxX4KQypViiFbeb32dSySPp2lMj/KDUOYPQ42JtH0gGm7aRy/4/owlPQZDR6FG95 ebHDhCDIvwsHnJkfiUN7vO1+roKNsdRw+CQeIDbiAHZ+6smDu+3/lktc1HxXHtMqRZdXrflWJrA TFyk/KarEpEQqf6qC7HOqSndNm/pRsfOG6+eciaUuzBfYCFMftfKdfy3Fr7nm8E8M+36scc3Zbj ntPbfyDSwJMcoorJpv5AdPMHgP39at6nsDjCNUlMrf1lu2UqYsnM6USfo0xZ9SG+/UqxeCA92b/ U6jhNpQpXs67LUkEynQnQng+0A== X-Google-Smtp-Source: AGHT+IF+fpzjBBB6YeyymtumlEZZKxSxnSR50VLDDcz7cTTiXCu1OC4x4yglIdXa/eak/2nres1DNywCw0XJyRFHatU= X-Received: by 2002:a05:620a:2a07:b0:89f:7109:185f with SMTP id af79cd13be357-8c08fbba649mr4148870485a.31.1766866504119; Sat, 27 Dec 2025 12:15:04 -0800 (PST) Precedence: list list-help: list-unsubscribe: list-post: List-Id: x-ms-reactions: disallow MIME-Version: 1.0 References: In-Reply-To: Date: Sat, 27 Dec 2025 21:14:53 +0100 X-Gm-Features: AQt7F2pqDN_o7NJQjuVKe3qVlB-vmh1uW3p-L0fJf0RttpLC7geWV9NH4BAB1LM Message-ID: Subject: Re: [PHP-DEV] RFC Karma request: native defer keyword (Zig-style) To: PHP internals Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable From: tovilo.ilija@gmail.com (Ilija Tovilo) Hi Khaled On Fri, Dec 26, 2025 at 4:41=E2=80=AFPM Khaled Alam wrote: > I'd like to request RFC karma to publish an RFC proposing a native "defer= " feature and gather community feedback. RFC karma was granted. Good luck! > Inspired by Zig's "defer", it schedules a block to run when the current f= unction exits (return, exception, or natural end), executed in LIFO order, = aimed at safer cleanup without repetitive try/finally scaffolding. I've already shared some technical feedback on the PR, let me reiterate her= e. I think it's important that defers are executed on scope-end, rather than function-end. For functions with many loop iterations, this would otherwise schedule tons of defer executions for resources that could have long been released. Relatedly, the way this is currently implemented, defer referencing changing variables is ineffective. For example: function test() { for ($i =3D 0; $i < 10; $i++) { $resource =3D allocate_some_resource(); defer { close($resource); } } } is roughly equivalent to: function test() { for ($i =3D 0; $i < 10; $i++) { $resource =3D allocate_some_resource(); } for ($i =3D 0; $i < 10; $i++) { close($resource); } } Which is obviously incorrect, closing the same resource over and over again. Executing at end-of-scope would mostly resolve this issue. On a more technical level, the implementation mostly mirrors FAST_CALL/FAST_RET used for finally. This seems unnecessary. FAST_CALL/FAST_RET also already handle exceptions, which your implementation is currently missing. I believe `defer { } ` could mostly be sugar for `try { } finally { }`. Given those things are so similar, the RFC should make it clear why a new syntax is beneficial. I suppose it puts the alloc/free code lexically closer together, which might be worth it for some people. Ilija