|
| 1 | +// Package types provides type aliases and helpers for explicit error and nil handling. |
| 2 | +// |
| 3 | +// This package re-exports types from github.com/samber/mo for consistent usage |
| 4 | +// across the Meridian codebase. The samber/mo library provides battle-tested |
| 5 | +// implementations of functional programming primitives. |
| 6 | +// |
| 7 | +// # Option[T] - Explicit nil handling |
| 8 | +// |
| 9 | +// Option represents an optional value that may or may not be present. |
| 10 | +// Use it to eliminate nil pointer dereference bugs at compile time. |
| 11 | +// |
| 12 | +// import "github.com/samber/mo" |
| 13 | +// |
| 14 | +// // Creating Options |
| 15 | +// opt := mo.Some(42) // Contains a value |
| 16 | +// opt := mo.None[int]() // Empty option |
| 17 | +// |
| 18 | +// // Checking and extracting values |
| 19 | +// if opt.IsPresent() { |
| 20 | +// value := opt.MustGet() // Safe after IsPresent check |
| 21 | +// } |
| 22 | +// |
| 23 | +// // Safe extraction with defaults |
| 24 | +// value := opt.OrElse(0) // Returns 0 if None |
| 25 | +// |
| 26 | +// // Functional composition |
| 27 | +// doubled := opt.Map(func(x int) int { return x * 2 }) |
| 28 | +// |
| 29 | +// # Result[T] - Explicit error handling |
| 30 | +// |
| 31 | +// Result represents either a successful value or an error. |
| 32 | +// Use it to make error handling explicit and prevent ignored errors. |
| 33 | +// |
| 34 | +// import "github.com/samber/mo" |
| 35 | +// |
| 36 | +// // Creating Results |
| 37 | +// result := mo.Ok(42) // Success |
| 38 | +// result := mo.Err[int](errors.New("")) // Failure |
| 39 | +// |
| 40 | +// // Wrapping traditional Go returns |
| 41 | +// result := mo.TupleToResult(strconv.Atoi("123")) |
| 42 | +// |
| 43 | +// // Checking and extracting |
| 44 | +// if result.IsOk() { |
| 45 | +// value := result.MustGet() // Safe after IsOk check |
| 46 | +// } |
| 47 | +// |
| 48 | +// // Functional composition |
| 49 | +// mapped := result.Map(func(x int) int { return x * 2 }) |
| 50 | +// |
| 51 | +// # Why use mo types? |
| 52 | +// |
| 53 | +// - Compile-time safety: Prevents nil dereferences and ignored errors |
| 54 | +// - Self-documenting: Function signatures clearly show optional/fallible operations |
| 55 | +// - Composable: Chain operations without nested if/else blocks |
| 56 | +// - Zero overhead: Benchmarks show no performance cost vs traditional patterns |
| 57 | +// |
| 58 | +// For more details, see: https://github.com/samber/mo |
| 59 | +package types //nolint:revive // "types" is intentional, similar to go/types in stdlib |
0 commit comments