hcl
hcl copied to clipboard
Support plus operator for string concatenation
Inspired by code in https://github.com/VladRassokhin/intellij-hcl/issues/212
resource "aws_kms_alias" "main" {
name = "alias/${var.alias}"
}
resource "aws_kms_alias" "main" {
name = "alias/" + var.alias
}
Should be equivalent, though terraform validate
shows an error
Unsuitable value for left operand: a number is required.
Hi @VladRassokhin! Thanks for opening this feature request.
Because HCL is a language with automatic type conversions and type inference, we do not vary behavior of operators depending on operand types. In other words, the +
operator implies a conversion to number because "1" + "1"
must always evaluate to 2
, not "11"
.
String interpolation is the string concatenation operator in HCL. That is an intentional design decision and not something we plan to change. If you found documentation somewhere that implied that +
can be used for string concatenation, I'd appreciate if you could let me know where it is so I can correct it.
One solution comes into mind fast could be to drop automatic type conversion and force usage of 'tointeger' when conversion is needed.
Hi @VladRassokhin,
The automatic type conversions are an important part of the language (to change that now would be a breaking change for many existing Terraform configurations), were an intentional language design decision, and we don't consider it a problem in need of a solution.
The correct way to concatenate strings in HCL is via the interpolation syntax. There is no other syntax for string concatenation, and no plan to add any others. The person who opened VladRassokhin/intellij-hcl#212 is correct that the refactoring suggestion in the IntelliJ extension is incorrect and should be removed.
I too had this issue
i tried this
name = "_amazonses." + var.domain_name
Unsuitable value for left operand: a number is required.
So used this to fix it
name = "_amazonses.${var.domain_name}"
i have tried all these formates till i reached
on modules/doks_create/main.tf line 2, in resource "digitalocean_kubernetes_cluster" "primary":
│ 2: name = var.project_name + var.environment + var.cluster_name
│ ├────────────────
│ │ var.project_name is "DOKS"
│
│ Unsuitable value for left operand: a number is required.
╵
╷
│ Error: Invalid operand
│
│ on modules/doks_create/main.tf line 2, in resource "digitalocean_kubernetes_cluster" "primary":
│ 2: name = var.project_name + var.environment + var.cluster_name
│ ├────────────────
│ │ var.environment is "dev"
│
│ Unsuitable value for right operand: a number is required.
╵
╷
│ Error: Invalid operand
│
│ on modules/doks_create/main.tf line 7, in resource "digitalocean_kubernetes_cluster" "primary":
│ 7: name = "${var.project_name + var.environment + var.cluster_name}-nodepool"
│ ├────────────────
│ │ var.project_name is "doks"
│
│ Unsuitable value for left operand: a number is required.
╵
╷
│ Error: Invalid operand
i had to change to this format instead of above
name = "${var.project_name}-${var.environment}-${var.cluster_name}"
name = "${var.project_name}-${var.environment}-${var.cluster_name}-nodepool"