Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Add support for unpacking values when calling variadic functions #418

Closed
irh opened this issue Jan 24, 2025 · 0 comments · Fixed by #419
Closed

Add support for unpacking values when calling variadic functions #418

irh opened this issue Jan 24, 2025 · 0 comments · Fixed by #419
Labels
enhancement New feature or request syntax Issues related to Koto's syntax

Comments

@irh
Copy link
Contributor

irh commented Jan 24, 2025

There's currently no way to unpack variadic arguments, which makes it impossible to forward them to another variadic function:

f = |args...|
  for i, arg in args.enumerate()
    print '{i}: {arg}'
    
g = |args...| 
  f args

f 'one', 'two', 'three'
# 0: one
# 1: two
# 2: three

g 'one', 'two', 'three'
# 0: ('one', 'two', 'three')

To solve this an unpacking operator (...) could be introduced, which would unpack values when calling a function. Variadic arguments are tuples, but ... would be more useful if it could unpack any iterable value.

f = |args...|
  for i, arg in args.enumerate()
    print '{i}: {arg}'
    
g = |args...| 
  f args... # unpack the args before calling f

g 'one', 'two', 'three'
# 0: one
# 1: two
# 2: three

x = 'abc' # Strings are iterable
g x..., 'extra' # The unpacked value doesn't have to be in last position
# 0: a
# 1: b
# 2: c
# 3: extra

A technical limitation is that currently the Call and CallInstance ops includes the argument count, but a flag could be introduced that tells the runtime to treat the arg count as a register, allowing the compiler to emit unpacking ops which increment a dynamic argument count.

See #417 for background discussion.

@irh irh added enhancement New feature or request syntax Issues related to Koto's syntax labels Jan 24, 2025
@irh irh closed this as completed in #419 Jan 27, 2025
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
enhancement New feature or request syntax Issues related to Koto's syntax
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant