snapapps / edgy

a visual programming language inspired by Scratch
http://snapapps.github.io/
GNU Affero General Public License v3.0
51 stars 21 forks source link

Dictionary lookup issue #378

Closed stevenbird closed 8 years ago

stevenbird commented 8 years ago

The following code produces the error: no entry 2 in dict:

image

cyderize commented 8 years ago

I think performing a change v by 1 changes v into a number, where the dictionary wants a string key.

I'll have a think about what to do to fix this.

cyderize commented 8 years ago

Another thing about the behaviour of dictionaries is that for 'object' keys, to access an entry, the object passed must be the exact same object as used to set the entry.

E.g. dictbehaviour will not work (since the lists are not the same memory location).

This might be confusing to beginners, so we should probably have a think about that sort of behaviour as well. One option might be to convert all keys to strings - then it wouldn't matter if a number type were used to access a string-key value (or vice-versa) and you wouldn't need to use the exact objects to access entries with object keys. There might be other side effects to this though, so I'll investigate and see if there would be issues with this or if there are better options available.

stevenbird commented 8 years ago

Another possible solution is to maintain a mapping from the string back to the object, and to report the object for some methods (e.g. keys in dict _), but use the string version in others (e.g. get _ in dict _).

cyderize commented 8 years ago

Alternatively we could try to automatically convert all numeric-like things to numbers so that way the string "2" would be converted to the number 2 when either getting or setting an entry (much like what is currently done with node ids).

This would still cause non-identical objects to not be considered the same key, but would be an easy fix to the change by ... problem, avoiding the burden of an extra mapping.

stevenbird commented 8 years ago

Suppose we only do the conversion on strings that can be losslessly converted to integers. (i.e. s == str(int(s))