Skip to content
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

Strange behaviour of Array#findIndex #754

Closed
pfortini opened this issue Aug 9, 2019 · 6 comments
Closed

Strange behaviour of Array#findIndex #754

pfortini opened this issue Aug 9, 2019 · 6 comments
Labels

Comments

@pfortini
Copy link

pfortini commented Aug 9, 2019

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:

find(name: string): i32 {
	return this.state.findIndex(
		(glob): bool => {
			return glob.name == name;
		}
	);
}

However, I know it is there. And if I write a simple loop, as of below, i get the normal result I'd expect

find(name: string): i32 {
	for (let i = 0; i < this.state.length; i++) {
		if (this.state[i].name == name) return i;
	}
	return -1;
}

Am I missing something crucial here?

@MaxGraey
Copy link
Member

MaxGraey commented Aug 9, 2019

Could you provide more info? Or better make fiddle in [WebAssembly Studio]. Are you sure you use == (and not ===) in your original code?

@pfortini
Copy link
Author

pfortini commented Aug 9, 2019

Could you provide more info? Or better make fiddle in [WebAssembly Studio]. Are you sure you use == (and not ===) in your original code?

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:
changing the findviaIndex to make the name "hardcoded", as below, makes it work somehow...

findviaIndex(name: string): i32 {
    return this.state.findIndex((_glob): bool => { return _glob.name == "state" })
  }

@MaxGraey
Copy link
Member

MaxGraey commented Aug 10, 2019

Ahh, I got what is problem. AssemblyScript currently not support capturing lexical environment (closures). That's why state.findIndex(glob => glob.name == name) not work properly but state.findIndex(glob => glob.name == "state") worked.

@pfortini
Copy link
Author

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!

@mariusa
Copy link

mariusa commented Dec 16, 2022

3 years later...This still doesn't work state.findIndex(glob => glob.name == name) : ERROR AS100: Not implemented: Closures

Are these array methods unusable with variables? If so, do we need to implement our own functions, which would do for (let i=0; ...) ?

@sebastiantf
Copy link

Still unusable with closurs

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants