---
title: Expressions
description: "This page lists all the expressions in Tact"
---

import { Badge } from '@astrojs/starlight/components';

Every operator in Tact forms an expression, but there's much more to uncover, as Tact offers a wide range of expressive options to choose from.

:::note

  The current maximum allowed nesting level of expressions is 83. An attempt to write a deeper expression will result in a compilation error:

  ```tact
  fun elegantWeaponsForCivilizedAge(): Int {
      return
      ((((((((((((((((((((((((((((((((
          ((((((((((((((((((((((((((((((((
              (((((((((((((((((((( // 84 parens, compilation error!
                  42
              ))))))))))))))))))))
          ))))))))))))))))))))))))))))))))
      ))))))))))))))))))))))))))))))));
  }
  ```

:::

## Literals

Literals represent values in Tact. These are fixed values—not variables—that you _literally_ provide in your code. All literals in Tact are expressions themselves.

You can also call [extension functions](/book/functions#extensions) defined on certain [primitive types][p] directly on their corresponding literal values:

```tact
// Calling toString() defined for Int on an integer literal:
42.toString();

// Calling asComment() defined for String on a string literal:
"Tact is awesome!".asComment();
```

### Integer literals

Integer literals can be written in [decimal](/book/integers#decimal) (base $10$), [hexadecimal](/book/integers#hexadecimal) (base $16$), [octal](/book/integers#octal) (base $8$), and [binary](/book/integers#binary) (base $2$) notations:

* A [_decimal_ integer](/book/integers#decimal) literal is a sequence of digits ($\mathrm{0 - 9}$).

* A leading $\mathrm{0x}$ (or $\mathrm{0X}$) indicates a [hexadecimal integer](/book/integers#hexadecimal) literal. They can include digits ($\mathrm{0 - 9}$) and the letters $\mathrm{a - f}$ and $\mathrm{A - F}$. Note that the case of a character does not change its value. Therefore, $\mathrm{0xa}$ = $\mathrm{0xA}$ = 10 and $\mathrm{0xf}$ = $\mathrm{0xF}$ = 15.

* A leading $\mathrm{0o}$ (or $\mathrm{0O}$) indicates an [octal integer](/book/integers#octal) literal. They can include only the digits $\mathrm{0 - 7}$.

* A leading $\mathrm{0b}$ (or $\mathrm{0B}$) indicates a [binary integer](/book/integers#binary) literal. They can include only the digits $0$ and $1$.

:::caution
  Be wary that in Tact, integer literals with a leading $0$ are still considered decimals, unlike in JavaScript/TypeScript, where a leading $0$ indicates an octal!
:::

Some examples of integer literals:

```tact
// decimal, base 10:
0, 42, 1_000, 020

// hexadecimal, base 16:
0xABC, 0xF, 0x0011

// octal, base 8:
0o777, 0o001

// binary, base 2:
0b01111001_01101111_01110101_00100000_01100001_01110010_01100101_00100000_01100001_01110111_01100101_01110011_01101111_01101101_01100101
```

Read more about integers and the [`Int{:tact}`](/book/integers) type on the dedicated page: [Integers](/book/integers).

### Boolean literals

The [`Bool{:tact}`](/book/types#booleans) type has only two literal values: `true{:tact}` and `false{:tact}`.

```tact
true == true;
true != false;
```

Read more about booleans and the [`Bool{:tact}`](/book/types#booleans) type in the dedicated chapter: [Booleans](/book/types#booleans).

### String literals

A string literal is zero or more characters enclosed in double (`"`) quotation marks. All string literals are objects of the [`String{:tact}`][p] type.

```tact
"foo";
"1234";
```

Tact strings support a range of [escape sequences](https://en.wikipedia.org/wiki/Escape_sequence) starting with a backslash `\\` character:

* `\\{:tact}` — literal backslash
* `\"{:tact}` — double quote
* `\n{:tact}` — newline
* `\r{:tact}` — carriage return
* `\t{:tact}` — tab
* `\v{:tact}` — vertical tab
* `\b{:tact}` — backspace
* `\f{:tact}` — form feed
* `\x00{:tact}` through `\xFF{:tact}` — [code point](https://en.wikipedia.org/wiki/Code_point), must be exactly two hex digits long
* `\u0000{:tact}` through `\uFFFF{:tact}` — [Unicode code point][unicode], must be exactly four hex digits long
* `\u{0}{:tact}` through `\u{10FFFF}{:tact}` — [Unicode code point][unicode], can be from 1 to 6 hex digits long

[unicode]: https://en.wikipedia.org/wiki/Unicode#Codespace_and_code_points

```tact
// \\
"escape \\ if \\ you \\ can \\";

// \"
"this \"literally\" works";

// \n
"line \n another line";

// \r
"Shutters \r Like \r This \r One";

// \t
"spacing \t granted!";

// \v
"those \v words \v are \v aligned";

// \b
"rm\b\bcreate!";

// \f
"form \f feed";

// \x00 - \xFF
"this \x22literally\x22 works"; // \x22 represents a double quote

// \u0000 - \uFFFF
"danger, \u26A1 high voltage \u26A1"; // \u26A1 represents the ⚡ emoji

// \u{0} - \u{10FFFF}
"\u{1F602} LOL \u{1F602}"; // \u{1F602} represents the 😂 emoji

// This Unicode code point is outside of valid range 000000–10FFFF
"\u{FFFFFF}"; // COMPILATION ERROR!
```

:::note

  Read more about strings and the [`String{:tact}`][p] type:\
  [Primitive types in the Book][p]\
  [Strings and StringBuilders in the Reference](/ref/core-strings)

:::

### `null` literal

The `null{:tact}` value is written with a `null{:tact}` literal. It is **not** an [identifier](#identifiers) and does not refer to any object. It is also **not** an instance of a [primitive type][p]. Instead, `null{:tact}` represents a lack of identification and the intentional absence of any value.

```tact
let var: Int? = null; // variable which can hold a null value
var = 42;
if (var != null) {
    var!! + var!!;
}
```

Read more about working with `null{:tact}` on the dedicated page: [Optionals](/book/optionals).

### Map literals

<Badge text="Available since Tact 1.6.7" variant="tip" size="medium"/><p/>

Map literals create maps by enclosing a comma-delimited list of zero or more predefined key-value pairs in curly braces.

```tact
// A compile-time map literal
let myMap: map<Int as uint8, Int as int13> = map<Int as uint8, Int as int13> {
    // Key expression: Value expression
    1 + 2: 10 * pow2(3), // key 3, value 80
    1 + 3: 20 * pow2(4), // key 4, value 320
};
myMap.get(3)!!; // 80
myMap.get(4)!!; // 320
```

Read more: [Initialize a map with a literal](/book/maps#initialize).

:::note

  Support for runtime initialization values that are not resolved at [compile-time](/ref/core-comptime) is planned for future Tact releases.

:::

## Identifiers

An identifier is a sequence of characters in the code that _identifies_ a [variable](/book/statements#let), [constant](/book/constants), [map](/book/maps), a [function](/book/functions), as well as a [Struct][s], [Message][m], [contract](/book/contracts), [trait](/book/types#traits), or their fields and methods. Identifiers are case-sensitive and not quoted.

In Tact, identifiers may contain Latin lowercase letters `a-z`, Latin uppercase letters `A-Z`, underscores `_`, and digits $\mathrm{0 - 9}$, but may not start with a digit. No other symbols are allowed, and Unicode identifiers are prohibited.

Note that identifiers for [primitive types][p] start with an uppercase letter. User-defined [composite types](/book/types#composite-types), such as [Structs][s] and [Messages][m], must also be capitalized.

:::caution

  All identifiers starting with `__gen` and `__tact` are not allowed and are instead reserved for internal use by the compiler.

:::

## Instantiation

You can create instances of the following types:

* [Structs][s]
* [Messages][m]

```tact
struct StExample {
    fieldInit: Int = 1;
    fieldUninit: Int;
}

fun example() {
    // Instance with default value of fieldInit
    StExample { fieldUninit: 2 };

    // Instance with both fields set
    StExample {
        fieldInit: 0,
        fieldUninit: 2, // trailing comma is allowed
    };
}
```

## Field access

You can directly access fields of the following types:

* [Structs][s]
* [Messages][m]

```tact
struct StExample {
    fieldInit: Int = 1;
    fieldUninit: Int;
}

fun example(): Int {
    let struct: StExample = StExample { fieldUninit: 2 }; // instantiation

    struct.fieldInit;          // access a field
    return struct.fieldUninit; // return field value from the function
}
```

## Extension function call

[Extension functions](/book/functions#extensions) are defined only on specific types. They can be called similarly to method calls in many other languages:

```tact
42.toString(); // toString() is a stdlib function that is defined on Int type
```

## Static function call

A [global function](/book/functions#fun-global) or a [internal function](/book/functions#fun-internal) of a [contract](/book/contracts) can be called from anywhere in the function body:

```tact
contract ExampleContract {
    receive() {
        now(); // now() is a static function of stdlib
        let expiration: Int = now() + 1000; // operation and variable definition
        expiration = self.answerQuestion(); // internal function
    }
    fun answerQuestion(): Int {
        return 42;
    }
}
```

## `initOf` {#initof}

<Badge text="500+ gas" title="Uses 500 gas units or more" variant="danger" size="medium"/><p/>

The expression `initOf{:tact}` computes the initial state, i.e., `StateInit{:tact}`, of a [contract](/book/contracts):

```tact
//                     argument values of contract or init() parameters
//                     ↓   ↓
initOf ExampleContract(42, 100); // returns a Struct StateInit{}
//     ---------------
//     ↑
//     name of the contract
//     ↓
//     ---------------
initOf ExampleContract(
    42,  // first argument
    100, // second argument; a trailing comma is allowed
);
```

The `StateInit{:tact}` is a [struct][s] consisting of the following fields:

Field  | Type                  | Description
:----- | :-------------------- | :----------
`code` | [`Cell{:tact}`][cell] | The initial code of the [contract](/book/contracts) (compiled bitcode)
`data` | [`Cell{:tact}`][cell] | The initial data of the [contract](/book/contracts) (parameters of the [`init(){:tact}`](/book/contracts#init-function) function or [contract parameters](/book/contracts#parameters))

:::note

  For workchain 0, the [`Address{:tact}`][p] of the current contract obtained by calling the [`myAddress(){:tact}`](/ref/core-contextstate#myaddress) function is identical to the one that can be obtained by calling the [`contractAddress(){:tact}`](/ref/core-addresses#contractaddress) function with the initial state of the current contract computed via `initOf{:tact}`:

  ```tact {6}
  contract TheKingPrawn {
      receive("keeping the address") {
          let myAddr1 = myAddress();
          let myAddr2 = contractAddress(initOf TheKingPrawn());

          myAddr1 == myAddr2; // true
      }
  }
  ```

  However, if you only need the address of the current contract at runtime and not its `StateInit{:tact}`, use the [`myAddress(){:tact}`](/ref/core-contextstate#myaddress) function, as it consumes **significantly** less gas.

:::

## `codeOf` {#codeof}


<Badge text="500+ gas" title="Uses 500 gas units or more" variant="danger" size="medium"/>
<Badge text="Available since Tact 1.6" variant="tip" size="medium"/><p/>

The expression `codeOf{:tact}` returns a [`Cell{:tact}`][cell] containing the code of a [contract](/book/contracts):

```tact
codeOf ExampleContract; // a Cell with ExampleContract code
//     ---------------
//     ↑
//     name of the contract
```

If `codeOf{:tact}` is used for the current contract, its result is equivalent to calling [`myCode(){:tact}`](/ref/core-contextstate#mycode).

```tact
contract ExampleContract() {
    receive() {
        myCode() == codeOf ExampleContract; // true
    }
}
```

If you only need the code of a given contract and not its [`StateInit{:tact}`](#initof), prefer using `codeOf ContractName{:tact}` over [`initOf ContractName(param1, param2, ...){:tact}`](#initof) to **significantly** reduce gas usage.

[p]: /book/types#primitive-types
[cell]: /book/cells#cells
[s]: /book/structs-and-messages#structs
[m]: /book/structs-and-messages#messages
