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