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

Delay evaluation of undefined variables #9184

Open
smortex opened this issue Dec 14, 2023 · 2 comments
Open

Delay evaluation of undefined variables #9184

smortex opened this issue Dec 14, 2023 · 2 comments
Labels
enhancement New feature or request

Comments

@smortex
Copy link
Contributor

smortex commented Dec 14, 2023

Use Case

An undefined variable produce a warning. Later on, a value can be assigned to this variable, which somewhat clash with the fact that variables are immutable.

The following show illustrate this issue:

if defined('$foo') {
  warning("A: foo=${foo}")
} else {
  warning("A: foo is not defined")
}

$foo = 'value'

if defined('$foo') {
  warning("B: foo=${foo}")
} else {
  warning("B: foo is not defined")
}

Applying this catalog produce:

Warning: Scope(Class[main]): A: foo is not defined
Warning: Scope(Class[main]): B: foo=value

The above example is intended to illustrate the issue. In the wild, this problem is often found when a module provide a defined type which has parameters that default to the value of a variable in another class of the module, for example:

class stuff {
  $color = 'red'
}

define stuff::thing (
  Optional[String] $color = $stuff::color,
) {
  include stuff
  warning("Look, a ${color} ${title}!")
}

stuff::thing { 'something':
}
# Warning: Unknown variable: 'stuff::color'.
#=> "Look, a  something!"

Describe the Solution You Would Like

Instead of immediately producing a warning if a variable is used (or returning false when testing if the variable is defined), stopping evaluation of the current statement and re-evaluating it later may help have a better behavior.

Describe Alternatives You've Considered

If deferring evaluation is not possible, assigning a value to a variable which has already resolved as undefined should not be possible and raise an error.

@smortex smortex added the enhancement New feature or request label Dec 14, 2023
@tvpartytonight
Copy link
Contributor

@puppetlabs/devx do you have any thoughts about this setup exposing this behavior?

@hlindberg
Copy link
Contributor

Implementing that would completely change the order in which defines are evaluated and would most likely break a lot of existing code. There have been proposals in the past to make the puppet language completely functional and declarative. The puppet language is not declarative (although the resulting catalog is) and changing the overall order of evaluation would be too painful for everyone.

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

No branches or pull requests

3 participants