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 path4_unwrap_example_test.go
More file actions
123 lines (102 loc) · 3.05 KB
/
4_unwrap_example_test.go
File metadata and controls
123 lines (102 loc) · 3.05 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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
package mu_test
import (
"errors"
"fmt"
"github.com/appthrust/mu"
)
func ExampleOption_Unwrap() {
// Unwrap with Some value
some42 := mu.Some(42)
value, ok := some42.Unwrap()
fmt.Println("Some(42).Unwrap():", value, ok)
// Unwrap with None value
none := mu.None[int]()
value2, ok2 := none.Unwrap()
fmt.Println("None.Unwrap():", value2, ok2)
// Practical usage pattern
if val, present := some42.Unwrap(); present {
fmt.Println("Value is present:", val)
} else {
fmt.Println("Value is absent")
}
// Output:
// Some(42).Unwrap(): 42 true
// None.Unwrap(): 0 false
// Value is present: 42
}
func ExampleEither_UnwrapLeft() {
// UnwrapLeft with Left value
left := mu.Left[string, int]("error message")
value, ok := left.UnwrapLeft()
fmt.Println("Left(\"error message\").UnwrapLeft():", value, ok)
// UnwrapLeft with Right value
right := mu.Right[string, int](42)
value2, ok2 := right.UnwrapLeft()
fmt.Println("Right(42).UnwrapLeft():", value2, ok2)
// Output:
// Left("error message").UnwrapLeft(): error message true
// Right(42).UnwrapLeft(): false
}
func ExampleEither_Unwrap() {
// Unwrap with Right value
right := mu.Right[string, int](42)
value, ok := right.Unwrap()
fmt.Println("Right(42).Unwrap():", value, ok)
// Unwrap with Left value
left := mu.Left[string, int]("error")
value2, ok2 := left.Unwrap()
fmt.Println("Left(\"error\").Unwrap():", value2, ok2)
// Pattern matching with Either
either := mu.Right[string, int](100)
if rightVal, isRight := either.Unwrap(); isRight {
fmt.Println("Got right value:", rightVal)
} else if leftVal, isLeft := either.UnwrapLeft(); isLeft {
fmt.Println("Got left value:", leftVal)
}
// Output:
// Right(42).Unwrap(): 42 true
// Left("error").Unwrap(): 0 false
// Got right value: 100
}
func ExampleResult_Unwrap() {
// Unwrap with Ok value
ok := mu.Ok(42)
value, isOk := ok.Unwrap()
fmt.Println("Ok(42).Unwrap():", value, isOk)
// Unwrap with Err value
err := mu.Err[int](errors.New("operation failed"))
value2, isOk2 := err.Unwrap()
fmt.Println("Err.Unwrap():", value2, isOk2)
// Error handling pattern
result := mu.Ok(200)
if val, success := result.Unwrap(); success {
fmt.Println("Operation succeeded with value:", val)
} else {
fmt.Println("Operation failed")
}
// Output:
// Ok(42).Unwrap(): 42 true
// Err.Unwrap(): 0 false
// Operation succeeded with value: 200
}
func ExampleResult_UnwrapErr() {
// UnwrapErr with Err value
err := mu.Err[int](errors.New("something went wrong"))
errValue, hasErr := err.UnwrapErr()
fmt.Println("Err.UnwrapErr():", errValue.Error(), hasErr)
// UnwrapErr with Ok value
ok := mu.Ok(42)
errValue2, hasErr2 := ok.UnwrapErr()
fmt.Println("Ok(42).UnwrapErr():", errValue2 == nil, hasErr2)
// Error handling with UnwrapErr
result := mu.Err[string](errors.New("network timeout"))
if e, hasError := result.UnwrapErr(); hasError {
fmt.Println("Error occurred:", e.Error())
} else {
fmt.Println("No error")
}
// Output:
// Err.UnwrapErr(): something went wrong true
// Ok(42).UnwrapErr(): true false
// Error occurred: network timeout
}