Values

Aqua is all about combining data and computations. The runtime for the compiled Aqua code, AquaVM, tracks what data comes from what origin, which constitutes the foundation for distributed systems security. That approach, driven by π-calculus and security considerations of open-by-default networks and distributed applications as custom application protocols, also puts constraints on the language that configures it.

Values in Aqua are backed by VDS (Verifiable Data Structures) in the runtime. All operations on values must keep the authenticity of data, prooved by signatures under the hood.

That's why values are immutable. Changing the value effectively makes a new one:

x = "hello"
y = "world"

-- despite the sources of x and y, z's origin is "peer 1"
-- and we can trust value of z as much as we trust "peer 1"
on "peer 1":
  z <- concat(x, y)

More on that in the Security section. Now let's see how we can work with values inside the language.

Arguments

Function arguments are available within the whole function body.

func foo(arg: i32, log: string -> ()):
  -- Use data arguments
  bar(arg)

  -- Arguments can have arrow type and be used as strings
  log("Wrote arg to responses")

Return values

You can assign the results of an arrow call to a name and use this returned value in the code below.

Aqua functions may return more than one value.

Literals

Aqua supports just a few literals: numbers, quoted strings, booleans, and nil. You cannot init a structure in Aqua, only obtain it as a result of a function call.

Arithmetic operators

Aqua offers a list of arithmetic and logic operators, introduced in Aqua 0.7.1.

Collections

With Aqua it is possible to create a stream, fill it with values, and use in place of any collection:

Aqua provides syntax sugar for creating any of the collection types with [ ... ] for arrays, ?[ ... ] for optional values, *[ ... ] for streams.

The ?[] expression takes any number of arguments, but returns an optional value that contains only 0 or 1 value. This is done by trying to yield these values one by one. The first value that yields without an error will be added to the resulting option.

As of Aqua 0.6.3, it is not possible to get an element by index directly from the collection creation expression.

Getters

In Aqua, you can use a getter to peek into a field of a product or indexed element in an array.

Note that the ! operator may fail or halt:

  • If it is called on an immutable collection, it will fail if the collection is shorter and has no given index; you can handle the error with try or otherwise.

  • If it is called on an appendable stream, it will wait for some parallel append operation to fulfill, see Join behavior.

Assignments

Assignments, =, only give a name to a value with an applied getter or to a literal.

Constants

Constants are like assignments but in the root scope. They can be used in all function bodies, textually below the place of const definition. Constant values must resolve to a literal.

You can change the compilation results by overriding a constant but the override needs to be of the same type or subtype.

Constants are always UPPER_CASE.

Visibility scopes

Visibility scopes follow the contracts of execution flow.

By default, everything defined textually above is available below. With some exceptions.

Functions have isolated scopes:

For loop does not export anything from it:

Parallel branches have no access to each other's data:

Recovery branches in conditional flow have no access to the main branch as the main branch exports values, whereas the recovery branch does not:

Streams as literals

Stream is a special data structure that allows many writes. It has a dedicated article.

To use a stream, you need to initiate it at first:

One of the most frequently used patterns for streams is Conditional return.

You can create a stream with Collection creation operators.

Last updated

Was this helpful?