Skip to main content

coalesce Function

coalesce takes any number of arguments and returns the first one that isn't null or an empty string.

All of the arguments must be of the same type. OpenTofu will try to convert mismatched arguments to the most general of the types that all arguments can convert to, or return an error if the types are incompatible. The result type is the same as the type of all of the arguments.


Code Block
> coalesce("a", "b")
> coalesce("", "b")
> coalesce(1,2)

To perform the coalesce operation with a list of strings, use the ... symbol to expand the list as arguments:

Code Block
> coalesce(["", "b"]...)

OpenTofu attempts to select a result type that all of the arguments can convert to, so mixing argument types may produce surprising results due to OpenTofu's automatic type conversion rules:

Code Block
> coalesce(1, "hello")
> coalesce(true, "hello")
> coalesce({}, "hello")

Error: Error in function call

Call to function "coalesce" failed: all arguments must have the same type.
  • coalescelist performs a similar operation with list arguments rather than individual arguments.