On 06.11.22 02:00, William Edsall wrote:
> Hi,
> Just curious if the LDMud developers have ever considered using named
> function parameters instead of strictly positional parameters. To my
> knowledge, LDMud uses positional parameters today, is that correct?
>
> Positional:
> type function(parameter1, parameter2, parameter3) (must be in correct
> order)
>
> Named:
> type function(parameter2=x, parameter1=y) (order or omission doesn't
> matter)
There was a discussion about this here a while ago. Conclusion back then
was that it would be quite cumbersome to implement, mainly because of
call_other (where the compiler doesn't know the target function, as it
may come from a variable). But AFAIR it at least sparked the
implementation of default values for function arguments :-)
There are two easy ways around this in LPC though: simply pass a mapping
or a struct. Not exactly the same, but good enough. And the overhead
should be negligible in most cases.
IMHO named arguments are most suitable for optional, rarely used values.
If you find yourself in a situation where you have trouble keeping track
of your arguments, you're most probably passing too many arguments in
the first place (i.e. the function is doing too much and you should
consider splitting it up anyways). If that's not practical I more and
more find myself putting those rarely used things into an "options"
mapping at the very end of the argument list.
That said: maybe structs could be expanded for this purpose, so that the
fields get extracted into variables automatically. But I doubt that the
benefits are worth the effort. Plus it still doesn't solve the issue
with call_other, where the target function isn't known at compile time
and therefore the compiler cannot implicitly create a suitable struct.
cu
Invis @ Beutelland