new operator ':=', it assigns a value to a variable by deep copy
This commit is contained in:
@@ -0,0 +1,28 @@
|
||||
// Copyright (c) 2024-2026 Celestino Amoroso (celestino.amoroso@gmail.com).
|
||||
// All rights reserved.
|
||||
|
||||
// clone-value.go
|
||||
package kern
|
||||
|
||||
func Clone(v any) (c any) {
|
||||
if v == nil {
|
||||
return
|
||||
}
|
||||
switch unboxed := v.(type) {
|
||||
case int64:
|
||||
c = unboxed
|
||||
case float64:
|
||||
c = unboxed
|
||||
case string:
|
||||
c = unboxed
|
||||
case bool:
|
||||
c = unboxed
|
||||
case *ListType:
|
||||
c = unboxed.Clone()
|
||||
case *DictType:
|
||||
c = unboxed.Clone()
|
||||
default:
|
||||
c = v
|
||||
}
|
||||
return
|
||||
}
|
||||
+1
-1
@@ -155,7 +155,7 @@ func (dict *DictType) GetItem(key any) (value any, exists bool) {
|
||||
func (dict *DictType) Clone() (c *DictType) {
|
||||
c = newDict(nil)
|
||||
for k, v := range *dict {
|
||||
(*c)[k] = v
|
||||
(*c)[k] = Clone(v)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
@@ -133,6 +133,15 @@ func (ls1 *ListType) Equals(ls2 ListType) (answer bool) {
|
||||
return
|
||||
}
|
||||
|
||||
func (ls1 *ListType) Clone() (ls2 *ListType) {
|
||||
ls := make(ListType, len(*ls1))
|
||||
for i, item := range *ls1 {
|
||||
ls[i] = Clone(item)
|
||||
}
|
||||
ls2 = &ls
|
||||
return
|
||||
}
|
||||
|
||||
func (dict *ListType) IndexDeepSameCmp(target any) (index int) {
|
||||
var eq bool
|
||||
var err error
|
||||
|
||||
Reference in New Issue
Block a user