Assignment and scope

An assignment binds an identifier to a variable, option, or function. Every identifier in a program must be assigned.

Flux is lexically scoped using blocks:

  1. The scope of a preassigned identifier is in the universe block.
  2. The scope of an identifier denoting a variable, option, or function at the top level (outside any function) is the package block.
  3. The scope of the name of an imported package is the file block of the file containing the import declaration.
  4. The scope of an identifier denoting a function argument is the function body.
  5. The scope of an identifier assigned inside a function is the innermost containing block.

An identifier assigned in a block may be reassigned in an inner block with the exception of option identifiers. While the identifier of the inner assignment is in scope, it denotes the entity assigned by the inner assignment.

Note that the package clause is not an assignment. The package name does not appear in any scope. Its purpose is to identify the files belonging to the same package and to specify the default package name for import declarations.

Variable assignment

A variable assignment creates a variable bound to an identifier and gives it a type and value. A variable keeps the same type and value for the remainder of its lifetime. An identifier assigned to a variable in a block cannot be reassigned in the same block. An identifier can be reassigned or shadowed in an inner block.

VariableAssignment = identifier "=" Expression .
Examples of variable assignment

In this code snippet, n and m are defined in an outer block as integers. Within the anonymous function, n and m are defined as strings, but only within that scope. So while the function will return "ab", n and m in the outer scope are unchanged, remaining n = 1 and m = 2.

n = 1
m = 2
x = 5.4
f = () => {
    n = "a"
    m = "b"
    return n + m

Option assignment

OptionAssignment = "option" [ identifier "." ] identifier "=" Expression .

An option assignment creates an option bound to an identifier and gives it a type and a value. Options may only be assigned in a package block. Once declared, an option may not be redeclared in the same package block. An option declared in one package may be reassigned a new value in another. An option keeps the same type for the remainder of its lifetime.

// alert package
option severity = ["low", "moderate", "high"]
// foo package
import "alert"
option alert.severity = ["low", "critical"]  // qualified option
option n = 1
option n = 2
f = (a, b) => a + b + n
x = f(a:1, b:1) // x = 4

Was this page helpful?

Thank you for your feedback!

Introducing InfluxDB Clustered

A highly available InfluxDB 3.0 cluster on your own infrastructure.

InfluxDB Clustered is a highly available InfluxDB 3.0 cluster built for high write and query workloads on your own infrastructure.

InfluxDB Clustered is currently in limited availability and is only available to a limited group of InfluxData customers. If interested in being part of the limited access group, please contact the InfluxData Sales team.

Learn more
Contact InfluxData Sales

The future of Flux

Flux is going into maintenance mode. You can continue using it as you currently are without any changes to your code.

Flux is going into maintenance mode and will not be supported in InfluxDB 3.0. This was a decision based on the broad demand for SQL and the continued growth and adoption of InfluxQL. We are continuing to support Flux for users in 1.x and 2.x so you can continue using it with no changes to your code. If you are interested in transitioning to InfluxDB 3.0 and want to future-proof your code, we suggest using InfluxQL.

For information about the future of Flux, see the following:

State of the InfluxDB Cloud Serverless documentation

InfluxDB Cloud Serverless documentation is a work in progress.

The new documentation for InfluxDB Cloud Serverless is a work in progress. We are adding new information and content almost daily. Thank you for your patience!

If there is specific information you’re looking for, please submit a documentation issue.