Unpacking tuples into function arguments (formerly "Variadic arguments") #257

Open
opened 2022-04-07 01:40:24 +08:00 by lriesebos · 3 comments
Collaborator

I was wondering, is variadic arguments something that will be supported in NAC3? This feature is currently not available, but I can imagine usecases for this.

Currently if you would run the following program

from artiq.experiment import *


class ArtiqTestExperiment(EnvExperiment):

    def build(self):
        self.setattr_device('core')
        self.l = [(self.foo, (1,)), (self.bar, (1, 2))]

    @kernel
    def run(self):
        for fn, args in self.l:
            fn(*args)

    @kernel
    def foo(self, a):
        print('foo', a)

    @kernel
    def bar(self, a, b):
        print('bar', a, b)

an error will be raised

repository/artiq_test.py:15:16-15:17: error: variadic arguments are not supported
            fn(*args)
               ^~~~~
I was wondering, is variadic arguments something that will be supported in NAC3? This feature is currently not available, but I can imagine usecases for this. Currently if you would run the following program ```python from artiq.experiment import * class ArtiqTestExperiment(EnvExperiment): def build(self): self.setattr_device('core') self.l = [(self.foo, (1,)), (self.bar, (1, 2))] @kernel def run(self): for fn, args in self.l: fn(*args) @kernel def foo(self, a): print('foo', a) @kernel def bar(self, a, b): print('bar', a, b) ``` an error will be raised ``` repository/artiq_test.py:15:16-15:17: error: variadic arguments are not supported fn(*args) ^~~~~ ```
Owner

As mentioned before, NAC3's type system requires type annotations on every function argument. How would you reconcile that with variadic arguments?

If you look at the way Python supports this:
https://peps.python.org/pep-0484/#arbitrary-argument-lists-and-default-argument-values
...all variadic arguments must have the same type. It sounds possible to implement that in NAC3 as it is merely syntactic sugar. But I'm not sure if this is what you want.

As mentioned before, NAC3's type system requires type annotations on every function argument. How would you reconcile that with variadic arguments? If you look at the way Python supports this: https://peps.python.org/pep-0484/#arbitrary-argument-lists-and-default-argument-values ...all variadic arguments must have the same type. It sounds possible to implement that in NAC3 as it is merely syntactic sugar. But I'm not sure if this is what you want.
Author
Collaborator

I think I might have phrased my question wrong. I am not interested in having function signatures with variadic arguments, I am interested in expanding sequences of values into the positional arguments of a function. So I would like the following to work in a kernel:

args = (1.44, 1)
fn_with_two_arguments(*args)

I think I might have phrased my question wrong. I am not interested in having function signatures with variadic arguments, I am interested in expanding sequences of values into the positional arguments of a function. So I would like the following to work in a kernel: ```python args = (1.44, 1) fn_with_two_arguments(*args) ```
Owner

Sounds possible - and we support non-homogeneous tuples, so there wouldn't be additional type restrictions there.

Sounds possible - and we support non-homogeneous tuples, so there wouldn't be additional type restrictions there.
sb10q added this to the Beta milestone 2022-04-07 07:37:59 +08:00
lriesebos changed title from Variadic arguments to ~~Variadic arguments~~ Unpacking tuples into function arguments 2022-04-07 07:41:09 +08:00
lriesebos changed title from ~~Variadic arguments~~ Unpacking tuples into function arguments to Unpacking tuples into function arguments (formerly "Variadic arguments") 2022-04-07 07:41:28 +08:00
Sign in to join this conversation.
No Milestone
No Assignees
2 Participants
Notifications
Due Date
The due date is invalid or out of range. Please use the format 'yyyy-mm-dd'.

No due date set.

Dependencies

No dependencies set.

Reference: M-Labs/nac3#257
No description provided.