Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:119572 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 13313 invoked from network); 19 Feb 2023 07:32:01 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 19 Feb 2023 07:32:01 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id AAC0B18005D for ; Sat, 18 Feb 2023 23:31:59 -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=0.8 required=5.0 tests=BAYES_50,SPF_HELO_NONE, SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=no autolearn_force=no version=3.4.2 X-Spam-ASN: AS24940 138.201.0.0/16 X-Spam-Virus: No X-Envelope-From: Received: from swift.blarg.de (swift.blarg.de [138.201.185.127]) by php-smtp4.php.net (Postfix) with ESMTP for ; Sat, 18 Feb 2023 23:31:59 -0800 (PST) Received: from swift.blarg.de (swift.blarg.de [IPv6:2a01:4f8:c17:52a8::2]) (Authenticated sender: max) by swift.blarg.de (Postfix) with ESMTPSA id 17E0C4107A for ; Sun, 19 Feb 2023 08:31:58 +0100 (CET) Date: Sun, 19 Feb 2023 08:31:56 +0100 To: internals@lists.php.net Message-ID: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline Subject: What's the purpose of zend_result? From: max+php@blarg.de (Max Kellermann) 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