Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(docs): Update other constructor refs in docs to use initializer #5227

Merged
merged 5 commits into from
Mar 14, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,13 @@ Functions serve as the building blocks of smart contracts. Functions can be eith

For a more practical guide of using multiple types of functions, follow the [token tutorial](../../../tutorials/writing_token_contract.md).

Currently, any function is "mutable" in the sense that it might alter state. However, we also support support static calls, similarly to EVM. A static call is essentially a call that does not alter state (it keeps state static).
Currently, any function is "mutable" in the sense that it might alter state. However, we also support support static calls, similarly to EVM. A static call is essentially a call that does not alter state (it keeps state static).

## Constructors
## Initializer functions

Every smart contract has a private `constructor` function which is called when the contract is deployed.
Smart contracts may have one, or many, initializer functions which are called when the contract is deployed.

A special constructor function must be declared within a contract's scope.

A constructor doesn't have a name, because its purpose is clear: to initialize contract state.
In Aztec terminology, a constructor is always a 'private function' (i.e. it cannot be a public function).
A constructor behaves almost identically to any other function. It is just important for Aztec to be able to identify this function as special: it may only be called once, and will not be deployed as part of the contract.
Initializers are regular functions that set an "initialized" flag (a nullifier) for the contract. A contract can only be initialized once, and contract functions can only be called after the contract has been initialized, much like a constructor. However, if a contract defines no initializers, it can be called at any time. Additionally, you can define as many initializer functions in a contract as you want, both private and public.

## Oracles

Expand All @@ -29,4 +25,4 @@ Explore this section to learn:
- How to write a [constructor](./write_constructor.md)
- [Calling functions from within the same smart contract and from different contracts](./call_functions.md), including calling private functions from private functions, public from public, and even private from public
- [Oracles](../oracles/main.md) and how Aztec smart contracts might use them
- [How functions work under the hood](./inner_workings.md)
- [How functions work under the hood](./inner_workings.md)
Original file line number Diff line number Diff line change
Expand Up @@ -2,36 +2,31 @@
title: How to write a constructor
---

This page explains how to write a constructor function.
This page explains how to write a constructor (or initializer) function.

To learn more about constructors, read [this](./main.md#constructors).
To learn more about constructors/initializers, read [this](./main.md#initializer-functions).
critesjosh marked this conversation as resolved.
Show resolved Hide resolved

## Annotate with `#[aztec(private)]`
Initializers are regular functions that set an "initialized" flag (a nullifier) for the contract. A contract can only be initialized once, and contract functions can only be called after the contract has been initialized, much like a constructor. However, if a contract defines no initializers, it can be called at any time. Additionally, you can define as many initializer functions in a contract as you want, both private and public.

Currently, all constructors in Aztec must be private.
## Annotate with `#[aztec(private)]` and `#[aztec(initializer)]`

Define your constructor like so:

```rust
#[aztec(private)]
fn constructor()
```

## Option 1: Empty constructor

Your constructor does not need to do anything; you can leave it blank like so:
Define your initiaizer like so:

```rust
#[aztec(private)]
fn constructor() {}
#[aztec(initializer)]
fn constructor(){
// function logic here
}
```

## Option 2: Constructor with logic
## Initializer with logic

Constructors are commonly used to set an admin, such as this example:
Initializers are commonly used to set an admin, such as this example:

#include_code constructor /noir-projects/noir-contracts/contracts/token_contract/src/main.nr rust

Here, the constructor is calling a public function. It can also call a private function. Learn more about calling functions from functions [here](../functions/call_functions.md).
Here, the initializer is calling a public function. It can also call a private function. Learn more about calling functions from functions [here](../functions/call_functions.md).

To see constructors in action, check out the [Aztec.nr getting started guide](../../../getting_started/aztecnr-getting-started.md).
Loading