- OpenTofu 语言
- 函数
- coalesce
coalesce
函数
coalesce
接受任意数量的参数,并返回第一个非空或非空字符串的参数。
所有参数必须为相同类型。OpenTofu 将尝试将不匹配的参数转换为所有参数都可以转换到的最通用类型,或者如果类型不兼容则返回错误。结果类型与所有参数的类型相同。
示例
> coalesce("a", "b")
a
> coalesce("", "b")
b
> coalesce(1,2)
1
要使用字符串列表执行 coalesce
操作,请使用 ...
符号将列表扩展为参数
> coalesce(["", "b"]...)
b
OpenTofu 尝试选择所有参数都可以转换到的结果类型,因此混合参数类型可能会由于 OpenTofu 的自动类型转换规则而产生意外结果
> coalesce(1, "hello")
"1"
> coalesce(true, "hello")
"true"
> coalesce({}, "hello")
Error: Error in function call
Call to function "coalesce" failed: all arguments must have the same type.
相关函数
coalescelist
对列表参数而不是单个参数执行类似的操作。