Are maps with keys of type any usable?

Hi,

Map : [string]any = map{}
Map["Number"] = 1
Number : int = Map["Number"] ## Throws error cause Map["Number"] returns 'any' instead of 'int'.

Is there a way to cast the type any to a different type? Unless there is, it seems like using a map []any is not of any use.

Number : int = int[Map["Number"]] ## Also throws an error.

Which I don’t mind, but I do wonder why it is allowed, cause it’s not like you can give any use to the returned values if they can’t be casted. Or can you?

Thank you!

Not an answer to your question but I asked this here and waiting for an official answer:

Dynamic downcasting from any is currently not supported. This isn’t limited to maps or arrays, but generally. I hope we’ll get that feature before we get the new verse virtual machine update in 2024.

1 Like

Maps require that their keys are comparable for equality at runtime, and any includes values that aren’t. The comparable type captures exactly this constraint, so you should be able to use it as the key of a map: e.g. Map : [comparable]int.

From your example, it looks like you’re trying to use any for values in a map, not the keys. That is allowed, but because any isn’t even comparable, there’s very little you can do with those values. Using comparable for the value would be slightly more useful, but we don’t yet support dynamic downcasting from it.

See also: Is `false` a bottom type in Verse? - #4 by AndrewScheidecker