Re: RFC idea: using the void type to control maximum arity of user-defined functions

From: Date: Thu, 04 Apr 2024 14:36:39 +0000
Subject: Re: RFC idea: using the void type to control maximum arity of user-defined functions
References: 1 2 3  Groups: php.internals 
Request: Send a blank email to internals+get-122934@lists.php.net to get a copy of this message
Le 04/04/2024 à 16:29, Claude Pache a écrit :
Le 4 avr. 2024 à 15:03, Mark Trapp <mark@itafroma.com> a écrit :
On Thu, Apr 4, 2024 at 5:43 AM Pablo Rauzy<r@uzy.me> wrote: I like this concept, but instead of introducing a new syntax, have you considered leveraging attributes in the same way that PHP 8.3 introduced #[Override]? #[Nonvariadic] function foo () {} foo(42); // warning: foo() expects exactly 0 arguments, 1 given I think the intent would be clearer and it would avoid introducing a new syntax.
Indeed, except that I think it should be better to have *by default* the same behaviour between built-in functions and user-defined functions:
function foo() { }

foo(42);
// in PHP 8.x: Deprecated: foo() expects expects exactly 0 arguments, 1 given
// in PHP 9: Fatal error: foo() expects expects exactly 0 arguments, 1 given
and, in case you want a variadic function:
#[Variadic]
function foo() { }
Alternatively, we don’t really need to introduce a new attribute, because one can just write:
function foo(...$args) { }
—Claude
Hello Claude, I strongly agree in theory, but this could break existing code, and moreover such a proposal was already rejected: https://wiki.php.net/rfc/strict_argcount Regards, -- Pablo

« previous php.internals (#122934) next »