# Result

`Result[T]` is `ok(value)` or `error(...)`, which holds an [Error](https://www.agentica-lang.com/agentica/library/errors/error/index.md).

## ok

```signature
ok(value: T) -> Result[T]
ok() -> Result[Void]
```

```agentica
def main() -> Void
  puts(ok(3))
  puts(ok("yes"))
end
```

```output
ok(3)
ok("yes")
```

## error

```signature
error(message: String) -> Result[T]
error(message: String, kind: String, code: Int, details: Map[String, String], cause: Error) -> Result[T]
error(error: Error) -> Result[T]
```

With a `String`, it builds a new `Error` with that message, made at this call. The other fields come as named arguments, each optional and in any order; `code` and `cause` are stored as `some(...)` ([Error](https://www.agentica-lang.com/agentica/library/errors/error/index.md#kinds)). With an `Error`, it holds that same `Error`, so a `case` arm can pass an error on as a `Result` of another type. The type comes from an annotation, a return type, a parameter or a field.

```agentica
def fail_now() -> Result[Int] = error("broken")

def main() -> Void
  puts(fail_now())
end
```

```output
error("broken")
```

## Result.ok?

```signature
Result.ok?(result: Result[T]) -> Bool
```

```agentica
def main() -> Void
  puts("1".to_i.ok?)
  puts("one".to_i.ok?)
end
```

```output
true
false
```

## Result.unwrap!

```signature
Result.unwrap!(result: Result[T]) -> T
```

Gives the value, or stops the program with `RUNTIME-UNWRAP-ERROR`.

```agentica
def main() -> Void
  puts("5".to_i.unwrap! * 2)
end
```

```output
10
```

## Result.unwrap_or

```signature
Result.unwrap_or(result: Result[T], default: T) -> T
```

Gives the value, or the default for an error. The default is always computed.

```agentica
def main() -> Void
  puts("5".to_i.unwrap_or(0))
  puts("five".to_i.unwrap_or(0))
end
```

```output
5
0
```

## Result.message

```signature
Result.message(result: Result[T]) -> String
```

Gives the `message` of the error, or `""` for a success.

```agentica
def main() -> Void
  puts("five".to_i.message)
  puts("[" + "5".to_i.message + "]")
end
```

```output
cannot convert "five" to Int
[]
```
