Skip to content
","upvoteCount":1,"answerCount":1,"acceptedAnswer":{"@type":"Answer","text":"

Hello and thank you for your detailed example model.

\n

The first issue you encounter: having 11 instead of 2 is not really a bug but simply a problem of priority of operations. What is read by the interpreter is something like this:

\n
write \"some string\" + <float> + <float>;\n
\n

and the priority is this one:

\n
write ( (\"some string\" + <float>) + float)\n
\n

so in that context all + are to be interpreted as concatenation and not addition. You don't see that with the multiplication example because the multiplication has a higher priority than the addition, and so

\n
write \"some string\" + <float> * <float>;\n
\n

resolves like this:

\n
write (\"some string\" + (<float> * <float>));\n
\n

and so the concatenation is done after floats are multiplied together.
\nYou can fix that by forcing the order yourself with parenthesis or using the sample operator which will generate the prefixed string for you, for example:

\n
write sample(foo[\"b\"] + 1);\n
\n

Nonetheless your example underlined two real problems with manipulating maps of unknown objects:

\n
    \n
  1. when the element stored is an int, it is rightfully typed as an int, but as soon as you do an arithmetical operation on it, it is cast into a float
  2. \n
  3. when the map is accessed from inside an agent, there are some confusion about the type itself (this is your last line where the interpreter thinks it is an image)
  4. \n
\n

I will create proper issues to track them down and hopefully fix this quickly, in the meantime a simple workaround for you will be to force cast in the code to the type you want to interact with (so instead of foo[\"a\"] + 123, do int(foo[\"a\"]) + 123).

","upvoteCount":1,"url":"https://github.com/gama-platform/gama/discussions/384#discussioncomment-11593291"}}}

Strange behavior of map<XXX, unknown> #384

Answered by lesquoyb
hdelattr asked this question in Q&A
Discussion options

You must be logged in to vote

Hello and thank you for your detailed example model.

The first issue you encounter: having 11 instead of 2 is not really a bug but simply a problem of priority of operations. What is read by the interpreter is something like this:

write "some string" + <float> + <float>;

and the priority is this one:

write ( ("some string" + <float>) + float)

so in that context all + are to be interpreted as concatenation and not addition. You don't see that with the multiplication example because the multiplication has a higher priority than the addition, and so

write "some string" + <float> * <float>;

resolves like this:

write ("some string" + (<float> * <float>));

and so the concatenation is done a…

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by hdelattr
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants