Re: Forced named parameters

From: Date: Mon, 19 May 2025 16:37:21 +0000
Subject: Re: Forced named parameters
Groups: php.internals 
Request: Send a blank email to [email protected] to get a copy of this message

> Since PHP named args work similarly, borrowing syntax makes sense. It would look like
> 
>  function f(Type $pos1, Type $pos2, ...$v, Type $named1, Type $named2)
> 
> or without the variadic
> 
>  function f(Type $pos1, Type $pos2, ..., Type $named1, Type $named2)

Hi Anton, I really like the idea, but I wouldn't have the requirement implicitly bound to the
presence and position of a variadic parameter the same way Python does. That kinda feels like a hack
and doesn't follow the principle of least consternation.

Moreover, what if I don't need a variadic parameter, but would like to declare that the
function's interface requires named arguments (because I don't want to be tied to locking
parameters to specific positions).

So instead I'd propose to implement this using an attribute (e.g.
#[RequireNamedArgs]) instead. That way, you could even make it so that only specific
arguments are required to be named. It's also syntactically backwards compatible, so this
feature wouldn't make your code break compatibility with older versions of PHP, and could be
made available in older PHP versions using static analysis.

Alwin


Thread (2 messages)

« previous php.internals (#127403) next »