> Aztec.nr API reference, v6.0.0 (mainnet and testnet). Index: https://docs.aztec.network/aztec-nr-api/v6.0.0/llms.txt

# Primitive type Expr

Module: `std`

A compile-time, quoted, syntactically valid expression.

An `Expr` can be obtained using [`Quoted::as_expr`](/aztec-nr-api/v6.0.0/std/primitive.Quoted.md#as_expr):

```rust
fn main() {
    comptime {
        let expr = quote { 1 + 2 }.as_expr().unwrap();
    }
}
```

## Implementations

```rust
impl Expr
```

```rust
pub comptime fn as_array(self) -> Option<[Self]>
```

If this expression is an array literal `[elem1, ..., elemN]`, this returns a vector of each element in the array.

```rust
pub comptime fn as_assert(self) -> Option<AssertExpression>
```

If this expression is an assert, this returns its predicate and optional message.

```rust
pub comptime fn as_assert_eq(self) -> Option<AssertEqExpression>
```

If this expression is an assert_eq, this returns the left-hand-side and right-hand-side expressions, together with the optional message.

```rust
pub comptime fn as_assign(self) -> Option<AssignExpression>
```

If this expression is an assignment, this returns its left- and right-hand sides.

```rust
pub comptime fn as_binary_op(self) -> Option<BinaryOpExpression>
```

If this expression is a binary operator operation `<lhs> <op> <rhs>`, return the left-hand side, operator, and the right-hand side of the operation.

```rust
pub comptime fn as_block(self) -> Option<[Self]>
```

If this expression is a block `{ stmt1; stmt2; ...; stmtN }`, return a vector containing each statement.

```rust
pub comptime fn as_bool(self) -> Option<bool>
```

If this expression is a boolean literal, return that literal.

```rust
pub comptime fn as_cast(self) -> Option<CastExpression>
```

If this expression is a cast expression `expr as type`, returns the casted expression and the type to cast to.

```rust
pub comptime fn as_comptime(self) -> Option<[Self]>
```

If this expression is a `comptime { stmt1; stmt2; ...; stmtN }` block, return each statement in the block.

```rust
pub comptime fn as_constructor(self) -> Option<ConstructorExpression>
```

If this expression is a constructor `Type { field1: expr1, ..., fieldN: exprN }`, return the type and the fields.

```rust
pub comptime fn as_for(self) -> Option<ForExpression>
```

If this expression is a for statement over a single expression, return its identifier, iterable, and body.

```rust
pub comptime fn as_for_range(self) -> Option<ForRangeExpression>
```

If this expression is a for statement over a range, return the identifier, the range start, the range end, whether the range is inclusive, and the for loop body.

```rust
pub comptime fn as_function_call(self) -> Option<FunctionCallExpression>
```

If this expression is a function call `foo(arg1, ..., argN)`, return the function and a vector of each argument.

```rust
pub comptime fn as_if(self) -> Option<IfExpression>
```

If this expression is an `if condition { then_branch } else { else_branch }`, return the condition, then branch, and else branch. If there is no else branch, `None` is returned for that branch instead.

```rust
pub comptime fn as_index(self) -> Option<IndexExpression>
```

If this expression is an index into an array `array[index]`, return the array and the index.

```rust
pub comptime fn as_integer(self) -> Option<IntegerLiteral>
```

If this expression is an integer literal, return its value. Negative integers are encoded as the equivalent negative field value such that e.g. `-1i8` has a value of `-1` rather than the two's complement encoding of `255`.

```rust
pub comptime fn as_lambda(self) -> Option<LambdaExpression>
```

If this expression is a lambda, returns the parameters, return type and body.

```rust
pub comptime fn as_let(self) -> Option<LetExpression>
```

If this expression is a let statement, returns the let pattern as an `Expr`, the optional type annotation, and the assigned expression.

```rust
pub comptime fn as_member_access(self) -> Option<MemberAccessExpression>
```

If this expression is a member access `foo.bar`, return the struct/tuple expression and the field. The field will be represented as a quoted value.

```rust
pub comptime fn as_method_call(self) -> Option<MethodCallExpression>
```

If this expression is a method call `foo.bar::<generic1, ..., genericM>(arg1, ..., argN)`, return the receiver, method name, a vector of each generic argument, and a vector of each argument.

```rust
pub comptime fn as_repeated_element_array(self) -> Option<RepeatedElementArrayExpression>
```

If this expression is a repeated element array `[elem; length]`, return the repeated element and the length expressions.

```rust
pub comptime fn as_repeated_element_vector(
    self,
) -> Option<RepeatedElementVectorExpression>
```

If this expression is a repeated element vector `[elem; length]`, return the repeated element and the length expressions.

```rust
pub comptime fn as_vector(self) -> Option<[Self]>
```

If this expression is a vector literal `@[elem1, ..., elemN]`, return each element of the vector.

```rust
pub comptime fn as_tuple(self) -> Option<[Self]>
```

If this expression is a tuple `(field1, ..., fieldN)`, return each element of the tuple.

```rust
pub comptime fn as_unary_op(self) -> Option<UnaryOpExpression>
```

If this expression is a unary operation `<op> <rhs>`, return the unary operator as well as the right-hand side expression.

```rust
pub comptime fn as_unsafe(self) -> Option<[Self]>
```

If this expression is an `unsafe { stmt1; ...; stmtN }` block, return each statement inside in a vector.

```rust
pub comptime fn has_semicolon(self) -> bool
```

Returns `true` if this expression is trailed by a semicolon.

Example:

```rust
comptime {
    let expr1 = quote { 1 + 2 }.as_expr().unwrap();
    let expr2 = quote { 1 + 2; }.as_expr().unwrap();

    assert(expr1.as_binary_op().is_some());
    assert(expr2.as_binary_op().is_some());

    assert(!expr1.has_semicolon());
    assert(expr2.has_semicolon());
}
```

```rust
pub comptime fn is_break(self) -> bool
```

Returns `true` if this expression is `break`.

```rust
pub comptime fn is_continue(self) -> bool
```

Returns `true` if this expression is `continue`.

```rust
pub comptime fn quoted(self) -> Quoted
```

Returns this expression as a `Quoted` value. It's the same as `quote { $self }`.

```rust
pub comptime fn resolve(self, in_function: Option<FunctionDefinition>) -> TypedExpr
```

Resolves and type-checks this expression and returns the result as a `TypedExpr`.

The `in_function` argument specifies where the expression is resolved:

- If it's `none`, the expression is resolved in the function where `resolve` was called
- If it's `some`, the expression is resolved in the given function

If any names used by this expression are not in scope or if there are any type errors, this will give compiler errors as if the expression was written directly into the current `comptime` function.
