> For the complete documentation index, see [llms.txt](https://gessadavide.gitbook.io/yallo-lang/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://gessadavide.gitbook.io/yallo-lang/docs/contract-structure.md).

# Contract structure

A contract is composed by three blocks:

* field declarations
* constructor (optional)
* entries and views

```csharp
contract AContract {
    field a: nat;
    field b: string;

    constructor (z: nat) {
        this.a = z;
        this.b = "string";
    }

    entry anEntry(v: nat) {
        this.a = v;
        []
    }

    view aView(): nat {
        this.a
    }
}
```

## Fields

A field represents a named value in the contract storage:

```
field fieldName: fieldType;
```

## Constructor

A constructor is an optional block, which allows to build an initial storage value for the contract; if defined, all storage field should be assigned in the body. The constructor could receive parameters, used to compute the initial storage: this is useful when a contract want to instantiate another contract using *createContract*.

```csharp
constructor (aParam: atype) {
    this.field1 = aParam;
    this.field2 = (aParam, "aString);
}
```

## Entries and Views

Entries and views defined the callable functionalities of the smart contract; they have a name and they could receive parameters.

An entry body is essentially an expression which always evalutes to an *operation list*. Entry body expression can modify the contract storage using particular expression with side effects (assignments and type specific modifiers).

```csharp
entry anEntry (p: nat) {
    this.natField = p * 2n;
    []
}
```

Views instead, are syntatic sugar for the callback pattern and they're translated to normal entries in the interal AST. Views have a custom return type

```csharp
view aView (p: nat): nat {
    p * 2n;
}
```

Which is equivalent to:

```csharp
entry aView (p: nat, cb: nat contrac) {
    [ cb (p * 2n) ]
}
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://gessadavide.gitbook.io/yallo-lang/docs/contract-structure.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
