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

Allow constants #3109

Closed
tsujp opened this issue Jun 8, 2021 · 4 comments
Closed

Allow constants #3109

tsujp opened this issue Jun 8, 2021 · 4 comments
Labels
enhancement New feature or request investigate Needs: Author Feedback Awaiting feedback from the author of the issue Needs: Triage 🔍

Comments

@tsujp
Copy link

tsujp commented Jun 8, 2021

Is your feature request related to a problem? Please describe.

In order to perform the features outlined in #3110 the concept of an explicit constant is required.

The following is currently invalid, and introducing constants would allow it to become valid:

var somethingAllowed = [
  'A'
  'B'
  'C'
]
@allowed(somethingAllowed)
param something string

// now somethingAllowed can be examined elsewhere (read-only)

Error

Error BCP032: The value must be a compile-time constant.

Referring to somethingAllowed within the decorator @allowed().

Also attempted hacks like array(somethingAllowed) etc do not work. It must be a literal.

Describe the solution you'd like

Addition of a const keyword, so now const somethingAllowed = [ ... ] defines an array which cannot be altered in any way and is guaranteed constant at compile time.

Giving:

const somethingAllowed = [
  'A'
  'B'
  'C'
]
@allowed(somethingAllowed)
param something string

// now somethingAllowed can be examined elsewhere (read-only)
@alex-frankel
Copy link
Collaborator

Variables are not allowed to be referenced in parameters, but I think this is one of those cases we can "fake" because we should be able to treat that var as a compile-time constant. @shenglol / @anthony-c-martin / @majastrz - any idea if we can enable this?

FWIW, a var in bicep is conceptually already a const since variables in bicep cannot be reassigned.

@majastrz
Copy link
Member

majastrz commented Jun 9, 2021

Yup as long as the variable is a compile-time constant, it should be possible to inline it into parameters. (In the JSON, the value of the variable would be copied.)

(Separately, we'd probably need to rename the internal RequiresInlining flag because there are 2 different inlinings that would happen with basically opposite requirements.)

@shenglol
Copy link
Contributor

shenglol commented Jun 9, 2021

Yeah it's possible to inline a constant, but I would like to point out that if we implement #622 and union type declaration, the const variable can be replaced with a type:

type SomethingAllowed = 'A' | 'B' | 'C'
param something SomethingAllowed
// Or just
param something 'A' | 'B' | 'C'

@ghost
Copy link

ghost commented May 24, 2023

Hi tsujp, this issue has been marked as stale because it was labeled as requiring author feedback but has not had any activity for 4 days. It will be closed if no further activity occurs within 3 days of this comment. Thanks for contributing to bicep! 😄 🦾

@ghost ghost closed this as completed Jun 3, 2023
@ghost ghost locked as resolved and limited conversation to collaborators Jul 4, 2023
@StephenWeatherford StephenWeatherford added Needs: Author Feedback Awaiting feedback from the author of the issue and removed awaiting response labels Oct 13, 2023
This issue was closed.
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
enhancement New feature or request investigate Needs: Author Feedback Awaiting feedback from the author of the issue Needs: Triage 🔍
Projects
Archived in project
Development

No branches or pull requests

5 participants