opa
opa copied to clipboard
Allow user-defined zero-argument functions in Rego
It's possible to declare functions with zero arguments in Rego:
package example
foo() := 1
these are however treated as regular rules, and there is no semantic difference between the above policy and:
package example
foo := 1
This is cause for confusion, as the user likely expects foo() to behave as a function (e.g. not contribute to output document), but it will actually behave as a rule.
Alternatively, it should be a parser error to declare a zero-arg rule.
Alternatively, it should be a parser error to declare a zero-arg rule.
I would suggest taking a stand here instead. The title of this issue suggests as much :)
IMO unless we think there is an actual need for user-defined zero-argument functions, this should be a parser error.
I would suggest taking a stand here instead. The title of this issue suggests as much :)
My intention was to bring this up for discussion. But in retrospect, I should have done that as a comment, and not as part of the description.
Yeah, no worries @johanfylling 🙂
@ashutosh-narkar seeing these used in policy libraries working on Regal integrations is what brought this up. Longer discussion on Slack, but since that's only retained for 90 days, motivation summarized for posterity. The arguments for allowing zero-arity functions:
- Not having to explain why zero-arity functions won't work, as there isn't really a technical reason.
- Consistency with built-in functions (
opa.runtime(),rego.metadata.rule(), etc) - Allows “masking”, or making values "private" when evaluated as part of larger document.
Thanks for the context @anderseknert! This list looks reasonable.
Make sure to add #6314 as a testcase for when this gets implemented.