Skip to content

Commit 6f97702

Browse files
committed
feat: add Clone method.
1 parent 75dc273 commit 6f97702

File tree

2 files changed

+21
-0
lines changed

2 files changed

+21
-0
lines changed

optional.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,13 @@ func Empty[T comparable]() *Optional[T] {
3131
return opt
3232
}
3333

34+
// Clone creates and returns a copy of this optional value.
35+
func (opt *Optional[T]) Clone() *Optional[T] {
36+
clone := new(Optional[T])
37+
clone.val = opt.val
38+
return clone
39+
}
40+
3441
// Equals indicates whether some other value is equals to this Optional.
3542
func (opt *Optional[T]) Equals(other any) bool {
3643
if opt == other {

optional_test.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,20 @@ func TestOptional(t *testing.T) {
100100
a.EqualNow(opt.OrElse("default"), "some text")
101101
}
102102

103+
func TestOptionalClone(t *testing.T) {
104+
a := assert.New(t)
105+
106+
opt := optional.Empty[string]()
107+
cpy := opt.Clone()
108+
a.NotEqualNow(opt, cpy)
109+
a.EqualNow(cpy.IsEmpty(), cpy.IsEmpty())
110+
111+
opt = optional.Of("test")
112+
cpy = opt.Clone()
113+
a.NotEqualNow(opt, cpy)
114+
a.EqualNow(cpy.GetPanic(), opt.GetPanic())
115+
}
116+
103117
func TestOptionalEquals(t *testing.T) {
104118
a := assert.New(t)
105119

0 commit comments

Comments
 (0)