Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

The default keyword should be honored on functions #2445

Closed
tsandall opened this issue May 29, 2020 · 6 comments · Fixed by #6093
Closed

The default keyword should be honored on functions #2445

tsandall opened this issue May 29, 2020 · 6 comments · Fixed by #6093
Labels

Comments

@tsandall
Copy link
Member

tsandall commented May 29, 2020

If you define a function with a default value, the default should be honored if all of the functions with the same name are undefined.

package test

default f(x) = 0

f(x) = x { x > 0 }

Today the default is ignored. Example:

$ opa eval -d x.rego 'data.test.f(1)'
{
  "result": [
    {
      "expressions": [
        {
          "value": 1,
          "text": "data.test.f(1)",
          "location": {
            "row": 1,
            "col": 1
          }
        }
      ]
    }
  ]
}
$ opa eval -d x.rego 'data.test.f(-1)'
{}
@srenatus
Copy link
Contributor

Wasm does that already (see #3389).

@tsandall tsandall added the rego label Apr 16, 2021
@oren-zohar
Copy link
Contributor

@tsandall any workaround for this?

@tsandall
Copy link
Member Author

You can use else to define the default value:

package repl

f(x) = 1 {
        x > 1
}

else = 0 {
        true
}
> f(-1)
0

@oren-zohar
Copy link
Contributor

@tsandall thanks for the quick response!
Small question, why do we need to add the:

else = 0 {
        true
}

inside the else clause? seems redundant no?

Wouldn't it be more elegant to have it like that:

package repl

f(x) = 1 {
        x > 1
} else = 0

I'm asking this since I noticed that opa fmt -w adds the same thing into the else clause if it's empty, even though it works the same.

ashutosh-narkar added a commit to ashutosh-narkar/opa that referenced this issue Jul 14, 2023
Fixes: open-policy-agent#2445

Signed-off-by: Ashutosh Narkar <anarkar4387@gmail.com>
ashutosh-narkar added a commit to ashutosh-narkar/opa that referenced this issue Jul 19, 2023
Default functions satisfy the following properties:

* Same arity as other functions with the same name
* Arguments should only be plain variables ie. no composite values. For ex, default f([x]) = 1 is an invalid default function
* Variable names should not be repeated ie. default f(x, x) = 1 is an invalid default function

Fixes: open-policy-agent#2445

Signed-off-by: Ashutosh Narkar <anarkar4387@gmail.com>
ashutosh-narkar added a commit to ashutosh-narkar/opa that referenced this issue Jul 19, 2023
Default functions satisfy the following properties:

* Same arity as other functions with the same name
* Arguments should only be plain variables ie. no composite values. For ex, default f([x]) = 1 is an invalid default function
* Variable names should not be repeated ie. default f(x, x) = 1 is an invalid default function

Fixes: open-policy-agent#2445

Signed-off-by: Ashutosh Narkar <anarkar4387@gmail.com>
ashutosh-narkar added a commit that referenced this issue Jul 20, 2023
Default functions satisfy the following properties:

* Same arity as other functions with the same name
* Arguments should only be plain variables ie. no composite values. For ex, default f([x]) = 1 is an invalid default function
* Variable names should not be repeated ie. default f(x, x) = 1 is an invalid default function

Fixes: #2445

Signed-off-by: Ashutosh Narkar <anarkar4387@gmail.com>
@anderseknert
Copy link
Member

Checking in from my vacation, and I'm happy that you went with the conservative approach here and made default functions similar to default rules @ashutosh-narkar 👍

Looking at the PR though, I don't see anything added to the documentation around this. Was that added elsewhere?

@ashutosh-narkar
Copy link
Member

Hey @anderseknert good point. I'll update the docs to add a note about this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
Status: Done
Development

Successfully merging a pull request may close this issue.

5 participants