in lib/collections.go [1053:1091]
func sumList(arg ref.Val) ref.Val {
list, ok := arg.(traits.Lister)
if !ok {
return types.NoSuchOverloadErr()
}
if list.Size() == types.IntZero {
return types.NewErr("no sum of empty list")
}
var (
iSum int
fSum float64
hasInt, hasFloat bool
)
it := list.Iterator()
for i := 0; it.HasNext() == types.True; i++ {
elem := it.Next()
switch elem := elem.(type) {
case types.Double:
if hasInt {
return types.NewErr("no sum of mixed int and double: first mismatch at index %d", i)
}
hasFloat = true
fSum += float64(elem)
case types.Int:
if hasFloat {
return types.NewErr("no sum of mixed int and double: first mismatch at index %d", i)
}
hasInt = true
iSum += int(elem)
default:
return types.NewErr("no sum of list containing %T", elem)
}
}
if hasInt {
return types.Int(iSum)
}
return types.Double(fSum)
}