LearnDataOption and result

Option and result

Model missing values and failed work.

Some work may not have a value. Some work may fail. Musi makes both cases visible in the type.

let option := import "@std/option";
let result := import "@std/result";
let Option := option.Option;
let Result := result.Result;

let findPort () : Option[Int] := option.someOf[Int](8080);

let parsePort () : Result[Int, String] :=
  result.ok[Int, String](8080);

parsePort();

Option means present or missing

The standard library exports Option[T], someOf, and noneOf.

let option := import "@std/option";

let configured := option.someOf[Int](8080);
let missing := option.noneOf[Int]();
let port := configured.unwrapOr[Int](3000);

Use Option for lookup results, optional configuration, and values that may honestly be absent.

Result means success or error

Result[T, E] keeps a success value type and an error value type.

let result := import "@std/result";

let paid := result.ok[Int, String](1200);
let declined := result.err[Int, String]("card declined");
let amount := paid.unwrapOr[Int, String](0);

Use Result when a caller needs to know why work failed.

Match when policy matters

Fallback helpers are fine for simple defaults. Use match when each case needs clear behavior.

match configured (
| .Some(port) => port
| .None => 3000
);

Do not hide the missing case

Do not hide missing values in magic numbers like -1. Give the missing case a real shape.