xgo/objects/builtin_len.go
Daniel Kang 3f55a6b5b0
Modules (#5)
Implement module imports
2019-01-17 01:56:05 -08:00

20 lines
404 B
Go

package objects
import (
"fmt"
)
func builtinLen(args ...Object) (Object, error) {
if len(args) != 1 {
return nil, ErrWrongNumArguments
}
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())
}
}