luau-lang/luau

Provide optional function parameters by name

nightcycle opened this issue · 1 comments

This is a pattern I've seen in a few languages that I've enjoyed, and would combine well with this feature request. Basically if you have a function with a lot of optional parameters, you could call them by name rather than by order.

For example in python:

def some_function(a: int=6, b: int=7, c: int=5, d: int=3):
    return a+b+c+d

some_function(
    b=4,
    d=5
)

This behavior isn't necessarily a game-changer, but I think it would meaningfully make code more readable + discourage certain anti-patterns currently plaguing lua/luau code. The obvious example would be when you're creating a complex functional system. Often times, when I'm writing things functionally, I'm doing it for the speed benefits. However, as these functional interfaces evolve I've found that they often get increasing numbers of parameters attached to them. This can lead to situations where half of the parameters are being passed as nil. You could of course create a frozen table, but that works against the speed benefits of the original functional direction + I can't always trust people to remember to freeze the table, so either I waste time doing validation / assertion steps, or I run the risk that state could change internal to the function.

So yeah, I think this would be a good addition, thank you for your time!

We do not plan to include this feature in Luau, regardless of optional parameters being accepted of not.
I would suggest not submitting an RFC as well, since we did discuss this feature internally before and decided against it, so RFC will be rejected.

In the absence of this feature, we suggest passing in a table.