Skip to content

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

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?

Activity

  1. KieranP commented on Oct 27, 2025

    @KieranP
    Author

    According to the docs (https://www.assemblyscript.org/stdlib/map.html), it recommends using has first:

    private cell_at(x: u32, y: u32): Cell | null {
      const key = `${x}-${y}`
      if (this.cells.has(key)) {
        return this.cells.get(key)
      } else {
        return null
      }
    }

    But that obviously adds 2x the function invocations, both using find.

    It would be nice if Map.get allowed a second argument to specify the fallback value.

    get(key: K, fallback: F): V { 
      let entry = this.find(key, HASH<K>(key)); 
      if (!entry) {
        if (fallback) return fallback
        throw new Error(E_KEYNOTFOUND); // cannot represent `undefined` 
      }
      return entry.value; 
     } 
    private cell_at(x: u32, y: u32): Cell | null {
      return this.cells.get(`${x}-${y}`, null)
    }
  2. HerrCai0907 commented on Oct 27, 2025

    @HerrCai0907
    Member

    I agree with you. In the original design, I think AS just wants to avoid undefined because according to TS, here should be T | undefined instead of T | null.
    @MaxGraey @dcodeIO @CountBleck Do you think we can return T | null for map.get? Though it is different with TS but it has better performance.

  3. dcodeIO commented on Oct 27, 2025

    @dcodeIO
    Member

    Issues in play here:

    • A Map<K,i32> would need to return i32 | null, which is an invalid type.
    • A Map<K,V | null> would have an ambiguous null return.
    • Not all (future) Wasm types might be nullable.

    Providing a second argument to specify a default could work, but I think we'd need a separate method for it, say getWithDefault to not break the existing behavior.

  4. mattjohnsonpint commented on Oct 27, 2025

    @mattjohnsonpint
    Contributor

    I'd prefer a TryGet or MaybeGet pattern that gives both a boolean indicator of success and a value in the case of success.

    A GetWithDefault pattern has the problem of not being able to distinguish between the default value existing at the key in the map, or the entry not existing at all.

  5. Mopsgamer commented on Nov 11, 2025

    @Mopsgamer
    Contributor
  6. JairusSW commented on Nov 18, 2025

    @JairusSW
    Contributor

    You can always use https://github.com/JairusSW/try-as in the meantime lol

  7. github-actions commented on Dec 18, 2025

    @github-actions

    This issue has been automatically marked as stale because it has not had recent activity. It will be closed in one week if no further activity occurs. Thank you for your contributions!

  8. KieranP commented on Dec 19, 2025

    @KieranP
    Author

    Still relevant

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions