Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature Request: Null Coalescing Operator #411

Open
filex opened this issue Oct 19, 2020 · 0 comments
Open

Feature Request: Null Coalescing Operator #411

filex opened this issue Oct 19, 2020 · 0 comments

Comments

@filex
Copy link

filex commented Oct 19, 2020

Many languages offer syntactic sugar for defining default values in form of a null coalescing operator. The most common one seems to be ??. go-cty offers the coalesce() function for that purpose.

The function works, but with a little sugar everything is sweeter :)

HCL Template

map = {
  attribute1 = null ?? "default"
  attribute2 = "Hello ${null ?? "world" }"
}

(In reality, instead of null we would use an expression that could actually yield a non-null result.)

Expected behavior

attribute1 should be assigned default
attribute2 should be assigned Hello world

This is the same behaviour as of cty's coalesce() function:

map = {
  attribute1 = coalesce(null, "default")
  attribute2 = "Hello ${coalesce(null, "world")}"
}

Obviously, "falsish" values, such as 0 or the empty string, should not be considered null. 0 ?? 1 should evaluate to 0.

This is a feature proposal for hcl2. As ?? is a parse error at the moment, it should be possible to add the operator without breaking existing configurations.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant