-
-
Notifications
You must be signed in to change notification settings - Fork 670
Strange behaviour of Array#findIndex #754
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Could you provide more info? Or better make fiddle in [WebAssembly Studio]. Are you sure you use |
I reproduced the error on WebAssembly Studio, but could not figure out how to share the project. However, this is the whole code you need to reproduce the error: export class glob {
name: string
constructor(name: string) {
this.name = name
}
}
export class MyClass {
state: Array<glob>
constructor() {
this.state = Array.create<glob>()
}
set(name: string): void {
this.state.push(new glob(name))
}
findviaIndex(name: string): i32 {
return this.state.findIndex((_glob): bool => { return _glob.name == name })
}
findviaLoop(name: string): i32 {
for (let i = 0; i < this.state.length; i++) {
if (this.state[i].name == name) return i
}
return -1
}
}
export function test(method: i32): i32 {
let myObj = new MyClass()
myObj.set("state")
if (method == 0) return myObj.findviaLoop("state")
return myObj.findviaIndex("state")
} calling for test(0) will use the loop method and return 0 as expected, but calling for test(1) will return -1 update: findviaIndex(name: string): i32 {
return this.state.findIndex((_glob): bool => { return _glob.name == "state" })
} |
Ahh, I got what is problem. AssemblyScript currently not support capturing lexical environment (closures). That's why |
Ohh I see... now I understand it. Perhaps there were even other parts of my code suffering from this and I didn't notice it. Thanks for the help! |
3 years later...This still doesn't work Are these array methods unusable with variables? If so, do we need to implement our own functions, which would do |
Still unusable with closurs |
I am having some strange behaviour on the Array#findIndex method. I have a class with the property "state" which is an array of objects containing the property "name".
When trying to implement a "find" method for my class, I got to this error. When using .findIndex I get the return -1, meaning that "name" could not be found. code below:
However, I know it is there. And if I write a simple loop, as of below, i get the normal result I'd expect
Am I missing something crucial here?
The text was updated successfully, but these errors were encountered: