tco icon indicating copy to clipboard operation
tco copied to clipboard

TCOs in functions with keywords

Open nijoakim opened this issue 6 years ago • 2 comments

Hey,

I'm trying to make the accumulator argument, k, in the simple factorial example implicit by using it as a keyword argument like so:

@with_continuations()
def factorial_kw(n, k=1, self=None):
	return self(n-1, k=n*k) if n > 1 else k
print(factorial_kw(10))

However, this results in the following error:

Traceback (most recent call last):
  File "./test.py", line 12, in <module>
    print(factorial_kw(10))
  File "/usr/local/lib/python3.6/dist-packages/tco/__init__.py", line 41, in __call__
    return f(*args)
  File "/usr/local/lib/python3.6/dist-packages/tco/__init__.py", line 86, in <lambda>
    f(*args, self=kself, **dict(zip(keys, conts)))) (*k)
  File "./test.py", line 11, in factorial_kw
    return self(n-1, k=n*k) if n > 1 else k
TypeError: t() got an unexpected keyword argument 'k'

Not sure whether this is expected or if it's a bug, so I figured I'd submit an issue.

nijoakim avatar Aug 16 '18 18:08 nijoakim

Hi, as stated in the title of your post, I am pretty sure the issue comes from the keyword argument. I think it was intended to be used with no keyword argument, but maybe you can achieve what you want by using the lower level C function. I will try to see if it works.

baruchel avatar Aug 17 '18 16:08 baruchel

Okay! Good to know that it's intended not to be used with keyword arguments. Maybe that should be stated somewhere.

nijoakim avatar Aug 26 '18 21:08 nijoakim