slyubomirsky commented on issue #14899:
URL: https://github.com/apache/tvm/issues/14899#issuecomment-1572540731
Implementing this kind of syntactic sugar would also be useful for purity
tracking, FYI. Unfortunately I'm not sure Python permits using `@R.function`
to be used with no arguments while also allowing for optional parameters.
Example:
```python
def dec(f, label=None):
def wrapper(*args, **kwargs):
if label is None:
print("No label")
else:
print(label)
return f(*args, **kwargs)
return wrapper
# this works as expected
@dec
def g(x):
return 1
# this will not work and will complain about lacking a positional argument
# @dec(label="hi")
# def h(x):
# return 2
```
This variant works but is a little cumbersome:
```python
def dd(label=None):
def dec(f):
def wrapper(*args, **kwargs):
if label is None:
print("No label")
else:
print(label)
return f(*args, **kwargs)
return wrapper
return dec
# has to be called like so...
@(dd())
def g(x):
return x
@(dd(label="hi"))
def h(x):
return x
```
Not sure if there's any way to avoid the nasty inner call :sweat_smile: (if
you leave off the extra parens, the function will be treated as the `label`
argument, so the results will not be good).
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]