Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:119573 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 18385 invoked from network); 19 Feb 2023 07:57:22 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 19 Feb 2023 07:57:22 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 5FEE618005D for ; Sat, 18 Feb 2023 23:57:21 -0800 (PST) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-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,FREEMAIL_FROM,HTML_MESSAGE, RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H3,RCVD_IN_MSPIKE_WL,SPF_HELO_NONE, SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=no autolearn_force=no version=3.4.2 X-Spam-ASN: AS15169 209.85.128.0/17 X-Spam-Virus: No X-Envelope-From: Received: from mail-qt1-f174.google.com (mail-qt1-f174.google.com [209.85.160.174]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange ECDHE (P-256) server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by php-smtp4.php.net (Postfix) with ESMTPS for ; Sat, 18 Feb 2023 23:57:17 -0800 (PST) Received: by mail-qt1-f174.google.com with SMTP id t10so165414qto.12 for ; Sat, 18 Feb 2023 23:57:17 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; t=1676793437; h=subject:to:from:date:references:in-reply-to:message-id:mime-version :user-agent:feedback-id:from:to:cc:subject:date:message-id:reply-to; bh=0hWBSMFKSa7EGdTXP4lSQYDIhoe78a44Eqhp9rv8FP8=; b=EKhF/3THCVllaT6ihzXZ4NwhDj8juQN62uq5dNMaP8IpsdxRtg2VjC9RBJTVqCivnj eLhoV71h2h0vJS7JeL2PDvdvQkVyvqCO5KQYtQH/Rk05eJvdmLvt9ijdkjqsXBslreaI vIQZVZy0xsVYte7jiOeOkSG96ttdrHWTUdPrpv5wBxx8yH/UBcgd3KpnpYoj+e4Z6+FI aO31cgCisXjdNEexhq4cnBn9ff7su8Tzhkddxz+Urv2sY+ktE3h4Cn+ayTgda0J5YW8I 0n1NDCPsElTnYZFWLjUS7BB8HPT4vMl1TH/m1FthHYa3IZDfBYea/ugHoBy2vMh2QCZV kNMw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1676793437; h=subject:to:from:date:references:in-reply-to:message-id:mime-version :user-agent:feedback-id:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=0hWBSMFKSa7EGdTXP4lSQYDIhoe78a44Eqhp9rv8FP8=; b=cA/LTFwj7bTydz/AMvYBBn7SKUMU32yGSSzRX5qdhDoaBE8Wroh0V0VJiVfPBElwG+ cPYeB1nI4ppoiw5t9gks+aTB0DhUNjl/nAsG8c4lw6uAaXBP4fvu7ePNeyhs17z8bsgT lMKuNkonnuDnGBDJOXPE70E248Gjn8gkCQ9uaZeewNAQ9SLMsiLNYrwyQs22pDZD0b5P 7wbwitr47iFEWmYb09k8oy24ZJcBl9x27ykZI7Bkt27MJGPxlvTPVoItFu8PjDbQEkuF I9+tnsLtdHqlWb3h3If4sSXYJIYj6g0s7ZczthioLP2XMA0W65SI6M9Jl9qVq2hWrNpY r6Bw== X-Gm-Message-State: AO0yUKW/moKK7iAQcDJogOabqMinKggLrNLj12XB9tl15IOOTkEUYyAe A4J0TBHXKBEhG8ycrV85M2dPdsEHNVw= X-Google-Smtp-Source: AK7set8WV42gI3Ji/+DBArOdJ1O+DIH0ZxPo914+RZ9hc5JMZfifrl4wPH4qggqSmsGUuZFIuBweQg== X-Received: by 2002:a05:622a:201:b0:3b8:6bef:61df with SMTP id b1-20020a05622a020100b003b86bef61dfmr13093277qtx.6.1676793436872; Sat, 18 Feb 2023 23:57:16 -0800 (PST) Received: from auth2-smtp.messagingengine.com (auth2-smtp.messagingengine.com. [66.111.4.228]) by smtp.gmail.com with ESMTPSA id o4-20020a37be04000000b00706b09b16fasm6790924qkf.11.2023.02.18.23.57.15 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 18 Feb 2023 23:57:16 -0800 (PST) Received: from compute2.internal (compute2.nyi.internal [10.202.2.46]) by mailauth.nyi.internal (Postfix) with ESMTP id CABA227C0054 for ; Sun, 19 Feb 2023 02:57:15 -0500 (EST) Received: from imap48 ([10.202.2.98]) by compute2.internal (MEProxy); Sun, 19 Feb 2023 02:57:15 -0500 X-ME-Sender: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedvhedrudejvddgudduvdcutefuodetggdotefrod ftvfcurfhrohhfihhlvgemucfhrghsthforghilhdpqfgfvfdpuffrtefokffrpgfnqfgh necuuegrihhlohhuthemuceftddtnecunecujfgurhepofgfggfkjghffffhvffutgesrg dtreerreertdenucfhrhhomhepfdfpihhkihhtrgcurfhophhovhdfuceonhhikhhithgr rdhpphhvsehgmhgrihhlrdgtohhmqeenucggtffrrghtthgvrhhnpedvudethfehveeltd dvlefghfetteetudefgfdtgeehffelhfdvheehueeihfdvheenucevlhhushhtvghrufhi iigvpedtnecurfgrrhgrmhepmhgrihhlfhhrohhmpehmrghilhdomhgvshhmthhprghuth hhphgvrhhsohhnrghlihhthidqudefudefledukeehiedqvdehkedvgeehieekqdhnihhk ihhtrgdrphhpvheppehgmhgrihhlrdgtohhmsehnphhophhovhdrtghomh X-ME-Proxy: Feedback-ID: id4a9467a:Fastmail Received: by mailuser.nyi.internal (Postfix, from userid 501) id 43D6D31A0063; Sun, 19 Feb 2023 02:57:15 -0500 (EST) X-Mailer: MessagingEngine.com Webmail Interface User-Agent: Cyrus-JMAP/3.9.0-alpha0-156-g081acc5ed5-fm-20230206.001-g081acc5e Mime-Version: 1.0 Message-ID: In-Reply-To: References: Date: Sun, 19 Feb 2023 08:56:54 +0100 To: "Levi Morrison" Content-Type: multipart/alternative; boundary=9e660f8b9770442480aa005b9bce48d8 Subject: Re: [PHP-DEV] What's the purpose of zend_result? From: nikita.ppv@gmail.com ("Nikita Popov") --9e660f8b9770442480aa005b9bce48d8 Content-Type: text/plain On Sun, Feb 19, 2023, at 08:31, Max Kellermann wrote: > Hi, > > I've done a bit of refactoring work around code using "zend_result", > but I keep wondering why it even exists. > > It was added in 1999 by commit 573b46022c46 in a huge 16k line commit > (as macros, converted to enum in 2012 by commit e3ef84c59bf). > > In 1999, C99 was brand new, and the "bool" type had just been > introduced to the C language - yes, C was 18 years old when a native > boolean type was added - but PHP didn't switch to C99 for another 19 > years later (b51a99ae358b). > > C had a long history of abusing "int" as boolean type, wasting 2 or 4 > bytes for something that could have easily fit in 1 byte. As if that > wasn't obscure enough, POSIX used 0 for success and -1 for error (and > missed the chance to return error codes, and instead added a global > variable for that which caused more headaches). (And don't get me > started on the horrible strcmp() return value.) Returning errors in C > is a huge obscure and inconsistent mess; every function does it > differently. > > This is PHP's original definition: > > #define SUCCESS 0 > #define FAILURE -1 /* this MUST stay a negative number, or it may effect functions! */ > > This appears to follow the POSIX school of bad error return values. > There's a comment which makes the thing even more obscure. > > Really, why does it need to be negative? > > Why does it imitate POSIX instead of using a boolean? (i.e. "int" and > non-zero for success for old-schoolers) > > The obvious way to indicate success or failure (without giving details > about the nature of the failure) would be to just return "bool". With > C99, any discussion on "0 or 1" vs "-1 or 0" is obsolete - there is > now a canonical boolean type that should be used. > > Of course, I know already that getting rid of "zend_result" in favor > of "bool" would be a major API breakage that requires careful > refactoring of almost all extensions. > > I just want to understand why "zend_result" was ever invented and why > it uses those surprising integer values. The commit message and that > code comment doesn't explain it. > > Rephrased: do you consider it a worthy goal to eventually get rid of > "zend_result", or do you believe it's good API design that should stay > forever? > > (Yet again I wish PHP was fully C++ - unlike C, C++ has strongly-typed > enums which cannot be casted implicitly to "int" or "bool"; that makes > refactoring a lot easier and safer.) > > Max Any type that has only two values is isomorphic to a boolean. However, for us humans, not all two-valued types are semantically equivalent. If you have a function like zend_hash_exists(), true and false are directly meaningful values. If you have a function like zend_stream_open_function(), SUCCESS and FAILURE are directly meaningful values. Now, if you make zend_stream_open_function() return a boolean instead, it's no longer clear what the return value means. Does a true return value mean that the stream was opened successfully, or that it failed? For a PHP programmer, that might sound silly -- of course true means success. However, in C the common error reporting convention is actually the other way around: Non-zero return values indicate failure. This means that false indicates success and true indicates failure. (I'm not kidding you -- I'm literally working on a project that uses boolean return values with this convention right now.) The current guideline for use of bool and zend_result in php-src is that bool is an appropriate return value for "is" or "has" style functions, which return a yes/no answer. zend_result is an appropriate return value for functions that perform some operation that may succeed or fail. I think that's a pretty reasonable state of things, and don't think there is a need to change it. Regards, Nikita --9e660f8b9770442480aa005b9bce48d8--