radon icon indicating copy to clipboard operation
radon copied to clipboard

Unlimited number of positional arguments and keyword arguments support in functions and methods.

Open Almas-Ali opened this issue 1 year ago • 11 comments

Is your feature request related to a problem? Please describe. Often we need to handle unknown amount of arguments in function call. But till now, we don't have any way to handle this behavior.

Describe the solution you'd like Syntax:

fun some(...args, ***kwargs)
{
    # args: [...]  Array
    # kwargs {...} HashMap
}

Inspired from Python.

Additional context Syntax is not final yet. Suggestions are welcome.

Update: New syntax is now:

  • [x] Positional arguments ...args (Updated with #153)
  • [ ] Keyword arguments ***kwargs
  • [ ] Array unpacking [...[1,2,3], ...[5,6,7]]
  • [ ] HashMap unpacking {***{"key": "value"}, ***{"key": "value"}}

Almas-Ali avatar May 31 '24 17:05 Almas-Ali

I like ... for positional args better. Not sure how to do kwargs in that case though (...... just looks stupid)

angelcaru avatar May 31 '24 18:05 angelcaru

Maybe ... for args and *** for kwargs?

angelcaru avatar May 31 '24 18:05 angelcaru

I was thinking to use ... for unpacking objects. Like [...[1,2,3], ...[5,6,7]]

Almas-Ali avatar May 31 '24 18:05 Almas-Ali

Then it's even better, because there's a nice symmetry between variadic arguments and passing variable amounts of args into a function. Like:

fun call(f, ...args, ***kwargs) -> f(...args, ***kwargs)

angelcaru avatar May 31 '24 18:05 angelcaru

fun call(f, ...args, ***kwargs) -> f(...args, ***kwargs)

Those number of stars looking weird to me.

Is it possible to use it also for unpacking objects like [...[1,2,3], ...[5,6,7]]?

Almas-Ali avatar May 31 '24 18:05 Almas-Ali

And also {***{"key": "value"}, ***{"key": "value"}} this syntax.

Almas-Ali avatar May 31 '24 18:05 Almas-Ali

The 3 *** is for symmetry with the 3 .... Also I don't think unpacking into lists and dicts is as important as into function calls because you can just do [1, 2, 3] + [4, 5, 6] while for function calls there is currently no way to do it at all. Not saying it's bad though

angelcaru avatar May 31 '24 18:05 angelcaru

I like this unpacking syntax from Python and also rest/spread operator from JS. I know developers will love it.

Almas-Ali avatar May 31 '24 18:05 Almas-Ali

I am convinced with you proposal of ... for positional arguments and *** for keyword arguments. Look fine to me! 👍🏼

Almas-Ali avatar May 31 '24 18:05 Almas-Ali

Updated the issue description and added list of tasks to perform.

Almas-Ali avatar May 31 '24 19:05 Almas-Ali

Next is keyword arguments!

Almas-Ali avatar Jun 08 '24 10:06 Almas-Ali