Bug in dictionaries

Hello, I recently discovered a bug in dictionaries, once there is a key that is a number (kept inside a math block) and then try to get the value of the key with a block of text inside which the number is (which is the key) then the value of the key is not accepted.
I have attached an aia file, I think it could best explain the problem
BUG.aia (2.3 KB)

I can confirm the stated behavior, and it is not discussed at
http://ai2.appinventor.mit.edu/reference/blocks/dictionaries.html#dictionaries

Here are the test components and results laid out for convenience:
Results Start

My takeaway from this test is that the dict component's key attributes incorporate type information in the lookup process, so numeric 1 and '1' are different keys.

Is it a bug, or is it a feature?

It is outside the spec linked above.

Regarding how it should be handled, AI2 has a half hearted history of trying to conform with Duck Typing in its math operations, but not in higher level things like lists and JSON.

So I guess my takeaway from this is:

Don't do that.

Dictionaries are implemented using Java's HashMap semantics currently, so it is the case that "1" and 1 are different values for keys because their types are fundamentally different (String vs Integer). We could consider changing the semantics (I would need to look into exactly how we could implement this). There's also the issue that in App Inventor "1" == 1 == 1.0 == "1.00", etc., so it's not just an issue of string<>integer mappings. There's also the challenge of the options blocks that @BeksOmega implemented where we decided that if an option doesn't exist in the dictionary we'd also check for the underlying value (e.g., HorizontalAlignment.Left == 1 but also VerticalAlignment.Top == 1).

1 Like