github.com/anth0d/nomad@v0.0.0-20221214183521-ae3a0a2cad06/website/content/docs/job-specification/hcl2/functions/conversion/can.mdx (about) 1 --- 2 layout: docs 3 page_title: can - Functions - Configuration Language 4 description: |- 5 The can function tries to evaluate an expression given as an argument and 6 indicates whether the evaluation succeeded. 7 --- 8 9 # `can` Function 10 11 `can` evaluates the given expression and returns a boolean value indicating 12 whether the expression produced a result without any errors. 13 14 This is a special function that is able to catch errors produced when evaluating 15 its argument. For most situations where you could use `can` it's better to use 16 [`try`](/docs/job-specification/hcl2/functions/conversion/try) instead, because it allows for more concise definition of 17 fallback values for failing expressions. 18 19 The `can` function can only catch and handle _dynamic_ errors resulting from 20 access to data that isn't known until runtime. It will not catch errors 21 relating to expressions that can be proven to be invalid for any input, such 22 as a malformed reference. 23 24 ~> **Warning:** The `can` function is intended only for simple tests in 25 variable validation rules. Although it can technically accept any sort of 26 expression and be used elsewhere in the configuration, we recommend against 27 using it in other contexts. For error handling elsewhere in the configuration, 28 prefer to use [`try`](/docs/job-specification/hcl2/functions/conversion/try). 29 30 ## Examples 31 32 ```shell-session 33 > local.foo 34 { 35 "bar" = "baz" 36 } 37 > can(local.foo.bar) 38 true 39 > can(local.foo.boop) 40 false 41 ``` 42 43 The `can` function will _not_ catch errors relating to constructs that are 44 provably invalid even before dynamic expression evaluation, such as a malformed 45 reference or a reference to a top-level object that has not been declared: 46 47 ```shell-session 48 > can(local.nonexist) 49 50 Error: Reference to undeclared local value 51 52 A local value with the name "nonexist" has not been declared. 53 ``` 54 55 ## Related Functions 56 57 - [`try`](/docs/job-specification/hcl2/functions/conversion/try), which tries evaluating a sequence of expressions and 58 returns the result of the first one that succeeds.