# Abi.array

An array of a Solidity type, of any length or of a fixed one.

## Usage

```ts
import { Effect } from "effect";
import { Abi } from "@ethect/core";

const Amounts = Abi.array(Abi.uint256); // uint256[]
const Pair = Abi.array(Abi.address, 2); // address[2]

const program = Effect.gen(function* () {
  const data = yield* Abi.encode(Amounts, [1n, 2n, 3n]);
  return yield* Abi.decode(Amounts, data); // [1n, 2n, 3n]
});
```

### Arrays of arrays

An array is a type, so it is an item too. Read it inside out, as Solidity does.

```ts
import { Abi } from "@ethect/core";

const Grid = Abi.array(Abi.array(Abi.bool, 3)); // bool[3][]: any number of rows of three
const Paths = Abi.array(Abi.array(Abi.address), 2); // address[][2]: two lists of any length
```

### Arrays of structs

```ts
import { Abi } from "@ethect/core";

const Order = Abi.struct({ maker: Abi.address, amount: Abi.uint256 });
const Orders = Abi.array(Order); // (address,uint256)[]
```

## Returns

[`Abi.Reference<ReadonlyArray<T>>`](https://ethect.dev/reference/abi/#reference-types)

The array type. A value is a readonly array of the item's values. It is an `Abi.Type` that says
it is a reference type.

## Parameters

### item

- **Type:** `Abi.Type<T>`

The type of every item: an elementary type, a struct, a tuple, another array, or a
[custom type](https://ethect.dev/reference/abi/#custom-types).

### length (optional)

- **Type:** `number`

The number of items, for `T[length]`. Left out, the array is `T[]` and takes any number.

```ts
import { Effect } from "effect";
import { Abi } from "@ethect/core";

const Pair = Abi.array(Abi.uint8, 2);
const failure = Effect.flip(Abi.encode(Pair, [1])); // an AbiError: two items are expected
```

## Behavior

- **A fixed length is checked.** A value of another length fails to encode. The types do not
  see it: a `T[2]` is a `ReadonlyArray<T>`, as a `T[]` is.
- **An empty array is a value** of `T[]`.
- **A length is a whole number of 1 or more.** Solidity has no `T[0]`. `Abi.array(item, 0)`
  does not throw: it gives a type whose every use fails with an `AbiError`.
- **An item that is not a Solidity type** gives such a type too.

## Errors

| Error | When |
| --- | --- |
| [`AbiError`](https://ethect.dev/reference/errors/abi-error/) | encoding a value that is not an array, or not of the fixed length |
| `AbiError` | encoding an item its type refuses. The message says which: `at [1]` |
| `AbiError` | decoding a length that the data cannot hold |
