type checking and error handling for builtins improved.
This commit is contained in:
@@ -1,6 +1,9 @@
|
||||
package builtins
|
||||
|
||||
import "monkey/object"
|
||||
import (
|
||||
"monkey/object"
|
||||
"monkey/typing"
|
||||
)
|
||||
|
||||
import (
|
||||
"strings"
|
||||
@@ -8,22 +11,19 @@ import (
|
||||
|
||||
// Join ...
|
||||
func Join(args ...object.Object) object.Object {
|
||||
if len(args) != 2 {
|
||||
return newError("wrong number of arguments. got=%d, want=1",
|
||||
len(args))
|
||||
if err := typing.Check(
|
||||
"join", args,
|
||||
typing.ExactArgs(2),
|
||||
typing.WithTypes(object.ARRAY_OBJ, object.STRING_OBJ),
|
||||
); err != nil {
|
||||
return newError(err.Error())
|
||||
}
|
||||
|
||||
if arr, ok := args[0].(*object.Array); ok {
|
||||
if sep, ok := args[1].(*object.String); ok {
|
||||
a := make([]string, len(arr.Elements))
|
||||
for i, el := range arr.Elements {
|
||||
a[i] = el.String()
|
||||
}
|
||||
return &object.String{Value: strings.Join(a, sep.Value)}
|
||||
} else {
|
||||
return newError("expected arg #2 to be `str` got got=%T", args[1])
|
||||
}
|
||||
} else {
|
||||
return newError("expected arg #1 to be `array` got got=%T", args[0])
|
||||
arr := args[0].(*object.Array)
|
||||
sep := args[1].(*object.String)
|
||||
a := make([]string, len(arr.Elements))
|
||||
for i, el := range arr.Elements {
|
||||
a[i] = el.String()
|
||||
}
|
||||
return &object.String{Value: strings.Join(a, sep.Value)}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user