This repository was archived by the owner on Aug 4, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1_types.go
More file actions
62 lines (57 loc) · 2.73 KB
/
1_types.go
File metadata and controls
62 lines (57 loc) · 2.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
package mu
// Unit represents a unit type, similar to void in other languages.
// It carries no information and is used when a type parameter is required
// but no meaningful value needs to be carried.
type Unit struct{}
// UnitValue is the singleton instance of [Unit] type.
// Use this instead of creating new `Unit{}` instances.
var UnitValue Unit
// Option represents an optional value: every `Option[T]` is either [Some] and contains a
// value, or [None] and does not. It provides a type-safe way to handle values
// that may be absent, avoiding the ambiguity of nil pointers for non-pointer types.
type Option[T any] struct {
// value holds the contained value when the option is [Some].
// When the option is [None], this field contains the zero value of T.
value T
// some indicates whether this option contains a value (true) or is empty (false).
// When true, the option is `Some(value)`; when false, the option is [None].
some bool
}
// Either represents a value of one of two possible types (a disjoint union).
// An instance of `Either[L, R]` is either a [Left] or a [Right]. This implementation is
// right-biased: methods such as `Map`, `FlatMap`, and `Get` operate on the [Right] side
// by default.
//
// A right-biased approach offers several advantages:
//
// 1. It follows the common convention where [Right] is the "happy-path" value
// and [Left] represents an alternative or error. This makes it natural to
// chain operations without repetitive branching.
// 2. Method signatures stay concise because they focus on the [Right] type,
// improving readability.
// 3. It allows Either to be treated as a monad, enabling fluent functional
// composition on the successful value.
//
// Either is still a general-purpose sum type and remains distinct from Result,
// which is specialized for success/failure semantics.
type Either[L any, R any] struct {
// left holds the [Left] value when isLeft is true.
// When isLeft is false, this field contains the zero value of L.
left L
// right holds the [Right] value when isLeft is false.
// When isLeft is true, this field contains the zero value of R.
right R
// isLeft indicates whether this Either contains a [Left] value (true) or [Right] value (false).
isLeft bool
}
// Result represents either success ([Ok]) or failure ([Err]). The error type is
// fixed to Go's built-in `error` interface to ensure maximum compatibility
// with the standard library and existing Go code.
type Result[T any] struct {
// value holds the success value when the result is [Ok].
// When the result is [Err], this field contains the zero value of T.
value T
// err holds the error when the result is [Err].
// When nil, the result is `Ok(value)`; when non-nil, the result is `Err(err)`.
err error
}