xgo/objects/builtin_len.go

25 lines
532 B
Go
Raw Normal View History

2019-01-09 10:17:42 +03:00
package objects
import (
"fmt"
)
func builtinLen(args ...Object) (Object, error) {
if len(args) != 1 {
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:
2019-01-15 10:52:52 +03:00
return &Int{Value: int64(len(arg.Value))}, nil
2019-01-09 10:17:42 +03:00
case *String:
2019-01-15 10:52:52 +03:00
return &Int{Value: int64(len(arg.Value))}, nil
case *Map:
return &Int{Value: int64(len(arg.Value))}, nil
case *ModuleMap:
return &Int{Value: int64(len(arg.Value))}, nil
2019-01-09 10:17:42 +03:00
default:
return nil, fmt.Errorf("unsupported type for 'len' function: %s", arg.TypeName())
}
}