# Method.overload

Groups several definitions of one JSON-RPC method into a single function whose result type
depends on its params. A call goes to the first variant whose params accept the arguments.

## Usage

```ts
import { Schema } from "effect";
import { Block, BlockId, Method } from "@ethect/core/rpc";

const tag = BlockId.numberOrTagSchema;
// the names of the params, said once for both variants
const define = Method.make<[block: unknown, full: unknown]>();

const getBlock = Method.overload(
  define("eth_getBlockByNumber", [tag, Schema.Literal(false)], Block.schema),
  define("eth_getBlockByNumber", [tag, Schema.Literal(true)], Block.fullSchema),
);

getBlock("latest", false); // Effect<Block.Block, …>: transactions as hashes
getBlock("latest", true); // Effect<Block.Full, …>: transactions in full
```

Each variant is a complete definition. TypeScript sees one function with one overload per
variant, in the order given, each under the [names](https://ethect.dev/rpc/method/make/#names) of its own
definition.

## Returns

[`Method.Overloaded<Variants, "method">`](https://ethect.dev/rpc/method/#types)

A function with the call signatures of every variant, plus two properties:

| Property | Type | Holds |
| --- | --- | --- |
| `method` | `string` | the wire name of the first variant |
| `variants` | a tuple of [`Method`](https://ethect.dev/rpc/method/) | the definitions, in order |

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

Eth.getBlockByNumber.method; // "eth_getBlockByNumber"
Eth.getBlockByNumber.variants.length; // 3
Eth.getBlockByNumber.variants[0].result; // the schema of Block | null
```

An overloaded method has no `params` and no `result` of its own: read them on a variant.

## Parameters

### variants

- **Type:** one or more [`Method`](https://ethect.dev/rpc/method/), as separate arguments

The definitions to group. They normally share a wire name and differ by a literal in their
params.

```ts
import { Schema } from "effect";
import { Hash } from "@ethect/core";
import { Method, Transaction } from "@ethect/core/rpc";

const full = Schema.Literal(true);
const hashes = Schema.optionalKey(Schema.Literal(false));

const pending = Method.overload(
  Method.make<[full: unknown]>()("acme_pending", [full], Schema.Array(Transaction.schema)),
  Method.make<[full?: unknown]>()("acme_pending", [hashes], Schema.Array(Hash.schema)),
);

pending(); // Effect<ReadonlyArray<Hash>, …>
pending(true); // Effect<ReadonlyArray<Transaction>, …>
```

## Behavior

- **First match wins.** At run time the arguments are tested against each variant's `params`
  schema in order, and the first that accepts them makes the call. Put the most specific
  variant first.
- **No match falls back to the first variant**, whose encoding then fails with a
  [`CodecError`](https://ethect.dev/reference/errors/codec-error/) that names the params that did not fit.
- **Trailing `undefined` is ignored** before matching, as in a plain call.
- **A value typed `boolean` matches neither a `true` nor a `false` variant.** Add a last
  variant that takes `Schema.Boolean` and returns the union, as the two block methods do. At
  run time the literal variants before it still make the call.

The library's overloaded methods are [`Eth.getBlockByNumber`](https://ethect.dev/rpc/eth/get-block-by-number/),
[`Eth.getBlockByHash`](https://ethect.dev/rpc/eth/get-block-by-hash/),
[`Eth.simulateV1`](https://ethect.dev/rpc/eth/simulate-v1/) and the
[`Debug.trace*`](https://ethect.dev/rpc/debug/tracers/) methods, which have one variant per tracer.
