You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
packer/website/content/docs/templates/hcl_templates/functions/conversion/convert.mdx

58 lines
1.7 KiB

---
page_title: convert function reference
description: The `convert` function converts a value or an expression to a given type.
---
⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️
> [!IMPORTANT]
> **Documentation Update:** Product documentation previously located in `/website` has moved to the [`hashicorp/web-unified-docs`](https://github.com/hashicorp/web-unified-docs) repository, where all product documentation is now centralized. Please make contributions directly to `web-unified-docs`, since changes to `/website` in this repository will not appear on developer.hashicorp.com.
⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️
# `convert` Function
`convert` converts a value or an expression to a given type.
Explicit type conversions are rarely necessary in HCL because it will convert
types automatically where required. Use the explicit type conversion functions
only to normalize types returned in outputs.
Only numbers and strings containing decimal representations of numbers can be
converted to number. All other values will produce an error.
Only boolean values and the exact strings "true" and "false" can be converted
to boolean. All other values will produce an error.
Only the primitive types (string, number, and bool) can be converted to string.
All other values will produce an error.
`convert(value, type_constraint)`
## Examples
```shell-session
> convert(3, string)
"3"
> convert("3", number)
3
> convert("false", bool)
false
> convert(false, string)
"false"
> convert(["a", "b", 3], list)
[
"a",
"b",
"3",
]
> convert(["c", "b", "b"], set)
[
"b",
"c",
]
> convert({"a" = "foo", "b" = true}, map)
{
"a" = "foo"
"b" = "true"
}
```