Le 04/04/2024 à 15:03, Mark Trapp a écrit :
On Thu, Apr 4, 2024 at 5:43 AM Pablo Rauzy<r...@uzy.me>  wrote:
Hello all,

First, I'm new here, so let me start by warmly thanking everyone
involved in PHP development. I've started programming 22 years using it
as my first programming language, and still enjoy using it a lot :).

I've read the rfc:howto and the first step is to get feedback from this
mailing list about the intended proposal, so here is a short description
of what I have in mind:

Currently PHP does not warn when user-defined functions are called with
too many arguments. Changing this could break a lot of existing code,
but it would also be a very good verification to catch potential bugs.

I have an idea allowing this feature without breaking any existing code:
using the void keyword in a function arguments list to strictly limit
the number of arguments a user-defined function can receive, stopping it
on the void, which could only be used once and in the last position.

Currently, void is a return-only type so it isn't used in arguments list
in existing code, so I believe the new behavior would only concern newly
written code that explicitly wants to take advantage of this feature.

A few examples using functions (the same would apply to class methods,
callbacks, closures, etc.) :

      function foo (void) {}
      foo(42); // warning: foo() expects exactly 0 arguments, 1 given

      function bar ($a, $b, void) {}
      bar(1, 2, 3); // warning: bar() expects exactly 2 arguments, 3 given

      function baz ($a, $b=null, void) {}
      baz(1, 2, 3); // warning: baz() expects at most 2 arguments, 3 given

I have no knowledge of the PHP internals: would that be feasible without
breaking things? And, as importantly, would it be a welcome change?

Cheers,
Hi Pablo,

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.

Hello Mark,

I never used attributes so I didn't even think of it, but that would work too, yes :).

I would personally prefer the void syntax, but that's really because attributes look odd to me due to lack of habits (and also because it avoids an additional line of code… which is probably not a solid enough reason when it comes to language design decisions).

Regards,

--
Pablo

Reply via email to