Types

Scalars

Scalar types follow the Wasm IT notation.
    Unsigned numbers: u8, u16, u32, u64
    Signed numbers: i8, i16, i32, i64
    Floats: f32, f64
    Boolean: bool
    String: string
    Records (product type): see below
    Arrays: see Collection Types below

Literals

You can pass booleans (true, false), numbers, double-quoted strings as literals.

Products

1
data ProductName:
2
field_name: string
3
​
4
data OtherProduct:
5
product: ProductName
6
flag: bool
Copied!
Fields are accessible with the dot operator . , e.g. product.field.

Collection Types

Aqua has three different types with variable length, denoted by quantifiers [], *, and ?.
Immutable collection with 0..N values: []
Immutable collection with 0 or 1 value: ?
Appendable collection with 0..N values: *
Any data type can be prepended with a quantifier, e.g. *u32, [][]string, ?ProductType are all correct type specifications.
You can access a distinct value of a collection with ! operator, optionally followed by an index.
It is possible to fill any collection with an empty one using nil.
Examples:
1
strict_array: []u32
2
array_of_arrays: [][]u32
3
element_5 = strict_array!5
4
element_0 = strict_array!0
5
element_0_anotherway = strict_array!
6
​
7
-- It could be an argument or any other collection
8
maybe_value: ?string
9
-- This ! operator will FAIL if maybe_value is backed by a read-only data structure
10
-- And will WAIT if maybe_value is backed with a stream (*string)
11
value = maybe_value!
12
​
13
-- Consider a function that takes a collection as an argument
14
func foo(a: ?string, b: []u32, c: *bool): ...
15
​
16
-- To call that function with empty collection, use nil:
17
foo(nil, nil, nil)
18
-- Nil fits into any collection
Copied!

Arrow Types

Every function has an arrow type that maps a list of input types to an optional output type.
It can be denoted as: Type1, Type2 -> Result
In the type definition, the absence of a result is denoted with (), e.g., string -> ()
The absence of arguments is denoted -> ().That is, this mapping takes no argument and has no return type.
Note that there's no Unit type in Aqua: you cannot assign a non-existing result to a value.
1
-- Assume that arrow has type: -> ()
2
​
3
-- This is possible:
4
arrow()
5
​
6
-- This will lead to error:
7
x <- arrow()
Copied!

Type Alias

For convenience, you can alias a type:
1
alias MyAlias : ?string
Copied!

Type Variance

Aqua is made for composing data on the open network. That means that you want to compose things if they do compose, even if you don't control its source code.
Therefore Aqua follows the structural typing paradigm: if a type contains all the expected data, then it fits. For example, you can pass u8 in place of u16 or i16. Or ?bool in place of []bool. Or *string instead of ?string or []string. The same holds for products.
For arrow types, Aqua checks the variance on arguments and contravariance on the return type.
1
-- We expect u32
2
xs: *u32
3
​
4
-- u16 is less then u32
5
foo1: -> u16
6
-- works
7
xs <- foo1()
8
​
9
-- i32 has sign, so cannot fit into u32
10
foo2: -> i32
11
-- will fail
12
xs <- foo2()
13
​
14
-- Function takes an arrow as an argument
15
func bar(callback: u32 -> u32): ...
16
​
17
​
18
foo3: u16 -> u16
19
​
20
-- Will not work
21
bar(foo3)
22
​
23
foo4: u16 -> u64
24
​
25
-- Works
26
bar(foo4)
Copied!
Arrow type A: D -> C is a subtype of A1: D1 -> C1, if D1 is a subtype of D and C is a subtype of C1.

Type Of A Service And A File

A service type is a product of arrows.
1
service MyService:
2
foo(arg: string) -> bool
3
​
4
-- type of this service is:
5
data MyServiceType:
6
foo: string -> bool
Copied!
The file is a product of all defined constants and functions (treated as arrows). Type definitions in the file do not go to the file type.
1
-- MyFile.aqua
2
​
3
func foo(arg: string) -> bool:
4
...
5
​
6
const FLAG ?= true
7
​
8
-- type of MyFile.aqua
9
data MyServiceType:
10
foo: string -> bool
11
flag: bool
Copied!
See Imports and Exports for module declarations.
aqua/Type.scala at main Β· fluencelabs/aqua
GitHub
See the types system implementation
Last modified 1mo ago