From e19d7514df498ea6405ecfce20086f93476bc218 Mon Sep 17 00:00:00 2001 From: Ashutosh Narkar Date: Thu, 20 Jul 2023 16:03:27 -0700 Subject: [PATCH] docs: Add a note about default functions Signed-off-by: Ashutosh Narkar --- docs/content/policy-language.md | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/docs/content/policy-language.md b/docs/content/policy-language.md index e94dcf83a2..e99948f656 100644 --- a/docs/content/policy-language.md +++ b/docs/content/policy-language.md @@ -1960,6 +1960,27 @@ a variable or reference. If the value is a composite then it may not contain variables or references. Comprehensions however may, as the result of a comprehension is never undefined. +Similar to rules, the `default` keyword can be applied to functions as well. + +For example: + +```live:eg/defaultfunc:module:read_only +default clamp_positive(x) := 0 + +clamp_positive(x) = x { + x > 0 +} +``` + +When `clamp_positive` is queried, the return value will be either the argument provided to the function or `0`. + +The value of a `default` function follows the same conditions as that of a `default` rule. In addition, a `default` +function satisfies the following properties: + +* same arity as other functions with the same name +* arguments should only be plain variables ie. no composite values +* argument names should not be repeated + ## Else Keyword The ``else`` keyword is a basic control flow construct that gives you control