# gizlang:core

Values that may fail or be absent. `try` unwraps the first case and returns any other case from the enclosing function.

```gizlang
import * as Core from "gizlang:core";
```

## Types

### `Result`

```gizlang
type Result<T, E> = | { kind: "ok"; value: T; } | { kind: "err"; error: E; }
```

The outcome of an operation that can fail: `ok` with the value, or `err` with the error.

### `Option`

```gizlang
type Option<T> = | { kind: "some"; value: T; } | { kind: "none"; }
```

An optional value: `some` with the value, or `none`.

### `Tuple2`

```gizlang
type Tuple2<A, B> = { first: A; second: B; }
```

TypeScript's tuples: `[A, B]` is Tuple2<A, B>, `t[0]` its `first`, and `[a, b]` where one is expected makes it.

Opaque: create and use it through this module's functions.

### `Tuple3`

```gizlang
type Tuple3<A, B, C> = { first: A; second: B; third: C; }
```

Three values of any types, as one value.

Opaque: create and use it through this module's functions.

### `Tuple4`

```gizlang
type Tuple4<A, B, C, D> = { first: A; second: B; third: C; fourth: D; }
```

Four values of any types, as one value.

Opaque: create and use it through this module's functions.

### `Tuple5`

```gizlang
type Tuple5<A, B, C, D, E> = { first: A; second: B; third: C; fourth: D; fifth: E; }
```

Five values of any types, as one value.

Opaque: create and use it through this module's functions.

### `Tuple6`

```gizlang
type Tuple6<A, B, C, D, E, F> = { first: A; second: B; third: C; fourth: D; fifth: E; sixth: F; }
```

Six values of any types, as one value.

Opaque: create and use it through this module's functions.

### `Error`

```gizlang
type Error = { name: string; message: string; cause: Option<string>; }
```

TypeScript's standard error: what `throw new Error("...")` throws and `throws Error` names. `name` is its kind ("Error",
or a domain's, such as "JsonError"), `message` says what went wrong, and `cause` is the message of the error under it.

- `name`: `string`
- `message`: `string`
- `cause`: `_gizlang_sdk_core.Option<string>`

## Functions

### `newError`

```gizlang
function newError(message: string): Error
```

`new Error(message)`.

### `raise`

```gizlang
function raise<E>(value: E): void throws E
```

Fails with VALUE: TypeScript's `throw value;` in a function that does not throw (parser throwValue), so the
failure faults R004 and prints VALUE, as a throw that nothing catches does.

### `moduleGet`

```gizlang
function moduleGet<T>(name: string, first: () => T): T
```

The value of the module-level `let` bound to `name` (compiler: checker moduleState); its first use makes it with
`first()`. A read of the name is this call.

### `moduleSet`

```gizlang
function moduleSet<T>(name: string, first: () => T, value: T): void
```

Gives the module-level `let` bound to `name` the value `value`: an assignment to the name is this call. `first` is
its initializer, which the assignment makes unneeded.

## Methods

### `Result.valueOr`

```gizlang
function Result.valueOr<T, E>(self: Result<T, E>, fallback: T): T
```

The value of a Result that succeeded, else `fallback`.

### `Result.unwrap`

```gizlang
function Result.unwrap<T, E>(self: Result<T, E>): T
```

The value, or fault R004 (a failed assert, with its call path) when there is
none: for a program that cannot go on without it, such as a script.

### `Option.unwrap`

```gizlang
function Option.unwrap<T>(self: Option<T>): T
```

The value of an Option; it is a defect (R004) when there is none.

### `Option.isSome`

```gizlang
function Option.isSome<T>(self: Option<T>): bool
```

Whether the Option has a value.

### `Result.isOk`

```gizlang
function Result.isOk<T, E>(self: Result<T, E>): bool
```

Whether the Result succeeded.

### `Option.map`

```gizlang
function Option.map<T, U>(self: Option<T>, f: (value: T) => U): Option<U> effects { f, fail }
```

Map the payload of a present value, preserving absence.

### `Option.andThen`

```gizlang
function Option.andThen<T, U>(self: Option<T>, f: (value: T) => Option<U>): Option<U> effects { f, fail }
```

Chain another optional computation without nesting the two cases.

### `Result.map`

```gizlang
function Result.map<T, E, U>(self: Result<T, E>, f: (value: T) => U): U throws E effects { f, fail }
```

Transform a successful value, preserving a typed error.

### `Result.mapErr`

```gizlang
function Result.mapErr<T, E, F>(self: Result<T, E>, f: (error: E) => F): T throws F effects { f, fail }
```

Change the error without touching a successful value.

### `Result.andThen`

```gizlang
function Result.andThen<T, E, U>(self: Result<T, E>, f: (value: T) => Result<U, E>): U throws E effects { f, fail }
```

Chain another fallible computation with the same error type.

