pocketlang icon indicating copy to clipboard operation
pocketlang copied to clipboard

Function with variadic parameters in script.

Open khchen opened this issue 2 years ago • 2 comments

Native function can have variadic parameters by using pkGetArgc. But it seems no way to have this ability it in script. Am I wrong?

khchen avatar Jun 19 '22 01:06 khchen

Yeah, at the moment it's not supported, thanks for the suggestion, I should definitely add this to the todo list.

ThakeeNathees avatar Jun 21 '22 22:06 ThakeeNathees

I can achieve similar behavior via callable object and native code in this branch.

I still have no idea how to implement in compiler and vm since the extra arguments must be discarded.

Example:

from types import Variadic

test = Variadic fn(arguments)
  print(arguments)
end

test(1)
test(1, 2, 3)

class Foo is Variadic
  def _init
    self.msg = "hello"
  end

  def _test1(arguments)
    print(self.msg, "test1", arguments)
  end

  def _test2(arguments)
    print(self.msg, "test2", arguments)
  end
end

f = Foo()
f.test1(1)
f.test2(1, 2, 3)

Output:

[1]
[1, 2, 3]
hello test1 [1]
hello test2 [1, 2, 3]

khchen avatar Jul 22 '22 14:07 khchen