xgo/objects/builtin_append.go

22 lines
473 B
Go
Raw Normal View History

2019-01-09 10:17:42 +03:00
package objects
// append(arr, items...)
2019-01-09 10:17:42 +03:00
func builtinAppend(args ...Object) (Object, error) {
if len(args) < 2 {
2019-01-17 12:56:05 +03:00
return nil, ErrWrongNumArguments
2019-01-09 10:17:42 +03:00
}
switch arg := args[0].(type) {
case *Array:
return &Array{Value: append(arg.Value, args[1:]...)}, nil
case *ImmutableArray:
return &Array{Value: append(arg.Value, args[1:]...)}, nil
2019-01-09 10:17:42 +03:00
default:
return nil, ErrInvalidArgumentType{
Name: "first",
Expected: "array",
Found: arg.TypeName(),
}
2019-01-09 10:17:42 +03:00
}
}