Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:127220 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 qa.php.net (Postfix) with ESMTPS id 795261A00BC for ; Mon, 28 Apr 2025 11:46:39 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=php.net; s=mail; t=1745840661; bh=8Wjjo0jE1TddkKxS+ra1sRUJ1cTxRTH8RyRtaSl6m4s=; h=References:In-Reply-To:From:Date:Subject:To:Cc:From; b=E+hHV0cww7OVfHMItvyOLy/dUzhwV2T/mpdStk9HyujWCNeAW+dy6BzPOw6EhFHta 6sBYsiBqp6gWarFSpfCrgH3oK2rjA+r0YWBQiNnCQ5o8j4DzjGehGhUep2fVDl2FtV 9DlvG66ZsjX7lpxSD+p9vgJiU6UsYKu0H1WuQzS/fwKdLffirSAFU7f/U6WKAeBtKr kQgDTSn1jSnWK2NXAiS4HFKn4aYT6Lkah31HDuFNzSBEBdF0Z0Uw+pZpwAZ7Tspa0E GJXCbjvloEuS73+11ytyqCxL0ZDZoerOXAeU7ccG3La7UdJjA669kRS3U3ne/Wp/PI hmCnp1NE3+qow== Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 1512318007D for ; Mon, 28 Apr 2025 11:44:21 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 4.0.0 (2022-12-13) 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, HTML_MESSAGE,RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H2,SPF_HELO_NONE, SPF_PASS autolearn=no autolearn_force=no version=4.0.0 X-Spam-Virus: Error (Cannot connect to unix socket '/var/run/clamav/clamd.ctl': connect: Connection refused) X-Envelope-From: Received: from mail-yb1-f169.google.com (mail-yb1-f169.google.com [209.85.219.169]) (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, 28 Apr 2025 11:44:20 +0000 (UTC) Received: by mail-yb1-f169.google.com with SMTP id 3f1490d57ef6-e732386e4b7so1777288276.1 for ; Mon, 28 Apr 2025 04:46:37 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1745840797; x=1746445597; darn=lists.php.net; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:from:to:cc:subject:date:message-id:reply-to; bh=5Dn+N1NF+zb1iFy7FNzJwh1Y90qDCkMLCRMfzamqxpA=; b=DPR607y5qNEKnjK7NhcSY/PCL/BUT7wTDNBJ7CFmTdmOZWyVByA04a6vq5DhXMehBB N/BuQTRWwHkC7UayisoL10e/wq4z73QiRYttdgWPJEFmPLMGf7eO/h8iYbNdLgpB/xuZ tePoAMZL7ZeQww7imGRjGLDDlgzoZ9cmyZSOczxxmQBmr6iF2Vvs4SGLHsxJNis7Qnvx 1KdpTxyJZM00zsyYyu+02tSm96S7cah0WUxBKngOiiGpIPLjQoGgYKbAv8WCnBfGAAd8 HWJ0x16cq4zfGf9FBcBk3LadwMQJ2NeRh6SZ5y3Avm3zrguC2VwJqL/YpFLrxZfMQQfZ kRJw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1745840797; x=1746445597; h=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=5Dn+N1NF+zb1iFy7FNzJwh1Y90qDCkMLCRMfzamqxpA=; b=hPTUuxwmIjpMUMF8XfWk0Q13O4mLR2QDhvcN6jOWknbsA+6bGSsmtWQFCvcAwRJ80S f2VGyM2RlVyNZ3ShaQEOwDNn5LRyL7dundYAgQJ+UZ3jxL2Mhos9nBuaxo7C7ATE3dnS +IsvFv2O+7cHuH8DwfwJmFPa8m1LZuHiatCPwV7ZLJUQMCCJHiTKt9ri8A9/VjTYxkWe 4NX+o9ogY975v5pc26uU6M/hTstcaiQ9TEidjsljn4vUqCueadttiRE1tufwgLbA13nN ZKFkOJ0Xa8MpsSZMcdhS2os4Sl3wKp2cTkw7n7PiUCepvbxypNwu13KggBeUHDq8kYEX VY6Q== X-Gm-Message-State: AOJu0Yy4zXbzK4y5KdVBLC07yh7KrzU1XrYGC64Z/veEUuBRGwua3Y2Z 0k7NCk5gIp91fJfSMz3JuGoaZ17RaYgPcF42NYS+S1INRMrAHFuz5U1deFMAt5GJGMl/NTGHXkd bZEnZvNYEQe8FPotOT/AWL6MpiPk= X-Gm-Gg: ASbGncuflRYLJgZZZoMUnTk/WfOm4+amU6WSeD04VF1zoJCAmdxhSKSHNIZC2XaEmtP xUvfjs3oZl5gHSKjhWcN+ehnU6lIHvCIVJfC+bJDXT7uqq5CM10VxL5AtONEO5y0A5kJXRmLUmU PaxgekS428HllsDhVC2z/E9w== X-Google-Smtp-Source: AGHT+IH3IVcWkdMfRaYo5q1IalWpakLRaqGtgApksvwzKUOdfb3XxXl6y9hOSup0t5CRhG1qZHqpgpb/VwQ3/zt0X4M= X-Received: by 2002:a05:6902:70b:b0:e65:11a5:790b with SMTP id 3f1490d57ef6-e732347ac32mr11542364276.44.1745840797208; Mon, 28 Apr 2025 04:46:37 -0700 (PDT) Precedence: bulk list-help: list-post: List-Id: internals.lists.php.net x-ms-reactions: disallow MIME-Version: 1.0 References: <39597a9c-6854-40c6-a529-32b2b178cb27@app.fastmail.com> In-Reply-To: Date: Mon, 28 Apr 2025 14:46:26 +0300 X-Gm-Features: ATxdqUG06UytJTO970N0PK6tP1zsmfHf2CU2cA9mCja7iFkZblvWOuERZuIi8nY Message-ID: Subject: Re: [PHP-DEV] Concept: Lightweight error channels To: Larry Garfield Cc: php internals Content-Type: multipart/alternative; boundary="0000000000008d5b920633d53cc0" From: edmond.ht@gmail.com (Edmond Dantes) --0000000000008d5b920633d53cc0 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable Hello all. > Yes. And even if it can be made faster (as it looks like Niels is doing, which is great), it will never be as fast as an empty constructor and a return. That's the level I'm proposing. If the backtrace is generated only when needed, rather than at the moment the exception is created, there will be no difference or almost no difference (if a stack unwinding algorithm is used). The total number of CPU/memory operations will be approximately the same. The only difference is that deferred backtrace generation will require more complex frame return code, which may introduce pitfalls. However, if a backtrace is needed by itself, such overhead is unavoidable regardless of whether exceptions are used or not. > I somewhat glossed over this point, but let me expand on it here. So the exception hierarchy in PHP is far from ideal. Understood. The hierarchy can be improved to make it more logical and consistent. And as I understand it, changing the exceptions themselves is not required for that. > Moreover, all exceptions currently track: Yes. And the main problem among them is the trace property. Neither file nor line cause significant overhead, because file is a reference to a string constant, and line is an integer. > This is incorrect, as making the current exception system checked would be a ginormous BC break. > And having some throwables be checked and some not, but using the same syntax and class hierarchy... well, that's the reason everyone detests Java's exceptions. Let's not do that. While introducing a new entity, such as a "New Error Stream" with defined contract rules, is theoretically possible, it would not fundamentally change the current situation. In practice, this approach would introduce a second category of exceptions, requiring developers to manage: - the primary execution flow, - the standard exception flow, - a "special exception flow" that must be converted back into the standard one when necessary. This added complexity may outweigh the potential benefits, especially given that the primary value would be limited to preserving backward compatibility. It is worth carefully considering whether such a change justifies the additional mental and technical overhead. On the other side of the scale: - replacing @throws with a special syntax (or perhaps simply introducing a new attribute?), - defining the behavior in case of contract violation while preserving backward compatibility. For example, if an exception contract is violated, an error would not be thrown; instead, a warning would be issued, which would not break the program's execution flow but would help draw the developer=E2=80=99s attent= ion to the issue. Later in the letter you explain in more detail that this is *not a special kind of exception*, nor a new execution flow, but rather a* special type of result*. But if this is a special type of result, then it should follow *the same rules as all PHP types*. In other words, this cannot be solved without generics. However, the benefit of the new syntax, which could make the code cleaner, does not depend on generics: For example: ```php $res =3D someFunction() catch ($err) {throw $err;} // Like Zig? ``` But then the return type must be Throwable. The advantage of generics like Result<> is that they do not create a separate return channel. Everything operates within a single flow. However, this approach is not yet possible in PHP. Introducing an additional return channel would mean increasing the overall complexity. Of course, this is a clean and useful syntax for such cases. Moreover, the catch block could be made mandatory if the function is marked with a contract =E2=80=94 meaning the function cannot be called without a c= atch block at all. However, it seems to me that we can achieve the same result using throw, simply by adding new syntax and capabilities. Yes, there may be some backward compatibility issues, but is it really something to be afraid of? --0000000000008d5b920633d53cc0 Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable
Hello all.

> Yes.=C2=A0 And even if = it can be made faster (as it looks like Niels is doing, which is great), it= will never be as fast as an empty constructor and a return.=C2=A0 That'= ;s the level I'm proposing.

If the backtrace i= s generated only when needed, rather than at the moment the exception is cr= eated, there will be no difference or almost no difference (if a stack unwi= nding algorithm is used).=C2=A0=C2=A0

The total nu= mber of CPU/memory operations will be approximately the same. The only diff= erence is that deferred backtrace generation will require more complex fram= e return code, which may introduce pitfalls.
However, if a backtrace is needed by itself, such overhead is unavoidable r= egardless of whether exceptions are used or not.=C2=A0=C2=A0

=
> I somewhat glossed over this point, but let me expand on it= here.

So the exception hierarchy in PHP is far from ideal. Understo= od. The hierarchy can be improved to make it more logical and consistent. A= nd as I understand it, changing the exceptions themselves is not required f= or that.=C2=A0=C2=A0

> Moreover, all exceptions= currently track:

Yes. And the main problem among them is the = trace property. Neither file nor line caus= e significant overhead, because file is a reference to a strin= g constant, and line is an integer.

&= gt; This is incorrect, as making the current exception system checked would= be a ginormous BC break.=C2=A0
> And having some throwables be chec= ked and some not, but using the same syntax and class hierarchy... well, th= at's the reason everyone detests Java's exceptions.=C2=A0 Let's= not do that.

While introducing a new entity, such a= s a "New Error Stream" with defined contract rules, is theoretica= lly possible,
it would not fundamentally change the current situation.

In practice, this approach would introduce a second cat= egory of exceptions, requiring developers to manage:

  • the primary execution flow,

  • the standard exception flow,

  • a "special exception flow" that must be conve= rted back into the standard one when necessary.

This added complexity may outweigh the potential benefi= ts, especially given that the primary value would be limited to preserving = backward compatibility.=C2=A0

It is worth carefully = considering whether such a change justifies the additional mental and techn= ical overhead.

On the other side of the scale:

  • replacing @throws with a special syntax (o= r perhaps simply introducing a new attribute?),

  • defining the behavior in case of contract violation whi= le preserving backward compatibility.

For example, if an = exception contract is violated, an error would not be thrown; instead, a wa= rning would be issued, which would not break the program's execution fl= ow but would help draw the developer=E2=80=99s attention to the issue.

Later in the letter you explain in more detail t= hat this is not a special kind of exception, nor a new execution flo= w, but rather a special type of result.=C2=A0=C2=A0

But if this is a special type of result, then it should follow = the same rules as all PHP types.=C2=A0 In other words, this cannot be solved without generics.

However, the benefit of the new syntax, which could make= the code cleaner, does not depend on generics:=C2=A0=C2=A0
For e= xample:=C2=A0=C2=A0

```php=C2=A0
$res =3D someF= unction() catch ($err) {throw $err;} // Like Zig?
```
But then the return type must be Throwable.=C2=A0= =C2=A0

The advantage of generics like R= esult<> is that they do not create a separate return channel.<= br> Everything operates within a single flow.
However, this approach is not yet possible in PHP.

Introducing an additional return channel would mean inc= reasing the overall complexity.

Of course= , this is a clean and useful syntax for such cases.
Moreover, the catch block could be made mandatory if the funct= ion is marked with a contract =E2=80=94 meaning the function cannot be call= ed without a catch block at all.

However, it seems to me that we can achieve the same re= sult using throw, simply by adding new syntax and capabilities= .=C2=A0 Yes, there may be some backward compatibility issues, but is it really some= thing to be afraid of?

--0000000000008d5b920633d53cc0--