rhai icon indicating copy to clipboard operation
rhai copied to clipboard

array.index_of: string argument got mistaken as function?

Open juchiast opened this issue 1 year ago • 3 comments

let array = ["hello"];
array.index_of("hello");
EXCEPTION: Function not found: hello (&str | ImmutableString | String, i64)
in call to function 'index_of' (line 2, position 7)

juchiast avatar Dec 16 '23 04:12 juchiast

I believe index_of takes a closure that matches items.

If you put in a string, it looks up a script function of the same name.

Thats why it is looking for a function called hello.

schungx avatar Dec 16 '23 10:12 schungx

There is an index_of that compares the passed value, only works with number

let array = [4, 5, 6, 19];
array.index_of(19);`

https://github.com/rhaiscript/rhai/blob/169af8eccbe7dab7125edecb6fa62afe199ccffd/src/packages/array_basic.rs#L823-L833

juchiast avatar Dec 16 '23 10:12 juchiast

It works for all types except string, where it got mistaken as name of a function...

The versions with function names as strings are deprecated but not yet removed for fear of breaking scripts.

Therefore unfortunately the depreciated version override the standard generic version.

schungx avatar Dec 16 '23 14:12 schungx