Skip to content

Map.get() always returns error, no way to catch it? #2956

@KieranP

Description

@KieranP

Question

I'm writing an AssemblyScript version of Conways Game of Life - https://github.com/KieranP/Game-Of-Life-Implementations

Part of it needs to fetch values from a Map but the key may not be there.

private cell_at(x: u32, y: u32): Cell {
  return this.cells.get(`${x}-${y}`)
}

However, AssemblyScript always throws an error instead of returning null.

get(key: K): V {
let entry = this.find(key, HASH<K>(key));
if (!entry) throw new Error(E_KEYNOTFOUND); // cannot represent `undefined`
return entry.value;
}

Map.find() is a private method, so I can't use that. And trying to use try/catch throws error ERROR AS100: Not implemented: Exceptions.

So how am I supposed to do this, try and fetch a map value, and handle when the map doesn't have the expected value?

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions