xgo/objects/builtin_len.go
2019-01-14 23:52:52 -08:00

20 lines
451 B
Go

package objects
import (
"fmt"
)
func builtinLen(args ...Object) (Object, error) {
if len(args) != 1 {
return nil, fmt.Errorf("wrong number of arguments (got=%d, want=1)", len(args))
}
switch arg := args[0].(type) {
case *Array:
return &Int{Value: int64(len(arg.Value))}, nil
case *String:
return &Int{Value: int64(len(arg.Value))}, nil
default:
return nil, fmt.Errorf("unsupported type for 'len' function: %s", arg.TypeName())
}
}