You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Map.get() always returns error, no way to catch it? #2956
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.
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.
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.
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!
Activity
KieranP commentedon Oct 27, 2025
According to the docs (https://www.assemblyscript.org/stdlib/map.html), it recommends using
hasfirst: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.
HerrCai0907 commentedon Oct 27, 2025
I agree with you. In the original design, I think AS just wants to avoid undefined because according to TS, here should be
T | undefinedinstead ofT | null.@MaxGraey @dcodeIO @CountBleck Do you think we can return
T | nullformap.get? Though it is different with TS but it has better performance.dcodeIO commentedon Oct 27, 2025
Issues in play here:
Map<K,i32>would need to returni32 | null, which is an invalid type.Map<K,V | null>would have an ambiguousnullreturn.Providing a second argument to specify a default could work, but I think we'd need a separate method for it, say
getWithDefaultto not break the existing behavior.mattjohnsonpint commentedon Oct 27, 2025
I'd prefer a
TryGetorMaybeGetpattern that gives both a boolean indicator of success and a value in the case of success.A
GetWithDefaultpattern 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.Mopsgamer commentedon Nov 11, 2025
See https://github.com/tc39/proposal-upsert
JairusSW commentedon Nov 18, 2025
You can always use https://github.com/JairusSW/try-as in the meantime lol
github-actions commentedon Dec 18, 2025
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!
KieranP commentedon Dec 19, 2025
Still relevant