type checking and error handling for builtins improved.
This commit is contained in:
@@ -2,20 +2,22 @@ package builtins
|
||||
|
||||
import (
|
||||
"monkey/object"
|
||||
"monkey/typing"
|
||||
"sort"
|
||||
)
|
||||
|
||||
// Sorted ...
|
||||
func Sorted(args ...object.Object) object.Object {
|
||||
if len(args) != 1 {
|
||||
return newError("wrong number of arguments. got=%d, want=1",
|
||||
len(args))
|
||||
if err := typing.Check(
|
||||
"sort", args,
|
||||
typing.ExactArgs(1),
|
||||
typing.WithTypes(object.ARRAY_OBJ),
|
||||
); err != nil {
|
||||
return newError(err.Error())
|
||||
}
|
||||
|
||||
if a, ok := args[0].(*object.Array); ok {
|
||||
newArray := a.Copy()
|
||||
sort.Sort(newArray)
|
||||
return newArray
|
||||
}
|
||||
return newError("argument #1 to `sorted` expected to be `array` got=%T", args[0].Type())
|
||||
arr := args[0].(*object.Array)
|
||||
newArray := arr.Copy()
|
||||
sort.Sort(newArray)
|
||||
return newArray
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user