2019-01-09 10:17:42 +03:00
|
|
|
package objects
|
|
|
|
|
2019-02-21 03:26:11 +03:00
|
|
|
// len(obj object) => int
|
2019-01-09 10:17:42 +03:00
|
|
|
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-02-02 04:32:05 +03:00
|
|
|
case *ImmutableArray:
|
|
|
|
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
|
2019-01-18 12:43:46 +03:00
|
|
|
case *Bytes:
|
|
|
|
return &Int{Value: int64(len(arg.Value))}, nil
|
2019-01-18 03:51:26 +03:00
|
|
|
case *Map:
|
|
|
|
return &Int{Value: int64(len(arg.Value))}, nil
|
2019-01-18 12:43:46 +03:00
|
|
|
case *ImmutableMap:
|
2019-01-18 03:51:26 +03:00
|
|
|
return &Int{Value: int64(len(arg.Value))}, nil
|
2019-01-09 10:17:42 +03:00
|
|
|
default:
|
2019-02-21 03:26:11 +03:00
|
|
|
return nil, ErrInvalidArgumentType{
|
|
|
|
Name: "first",
|
|
|
|
Expected: "array/string/bytes/map",
|
|
|
|
Found: arg.TypeName(),
|
|
|
|
}
|
2019-01-09 10:17:42 +03:00
|
|
|
}
|
|
|
|
}
|