Why Would a Random Integer be converted to Float?

29 views
Skip to first unread message

Gil Creque

unread,
Jun 29, 2022, 1:20:11 PM6/29/22
to blo...@googlegroups.com
I'm using the built in count and random integer blocks and for some reason when the random int is used in the count block it gets converted to a float. I'm not sure why it would do that. Is there a workaround?


blockly-demo.appspot.com_static_demos_code_index.html(720p).png
blockly-demo.appspot.com_static_demos_code_index.html(720p)-1.png

Neil Fraser

unread,
Jun 29, 2022, 2:09:19 PM6/29/22
to blo...@googlegroups.com
The reason for converting the endpoints of the loop to a number is that the loop was so that if those values are strings, they ought to still function as expected.
Screen Shot 2022-06-29 at 19.54.18.png
This mirrors the JavaScript approach of automatically casting values to a reasonable type.  And since loops can be incremented by non-integer values, floats are needed over ints.  Without those float() casts, the above blocks would throw TypeError: can only concatenate str (not "float") to str

However, literally none of the other blocks implement similar type casting.  Such as:
Screen Shot 2022-06-29 at 20.00.42.png

Furthermore, our style guide specifically states that we should generate clean code, and that we shouldn't attempt to make consistent execution across different languages.  It is clear to me that we are out of spec here.

I propose dropping the float() functions from the Python generator for this loop block.  A quick scan of the codebase doesn't reveal any similar cases.

--
You received this message because you are subscribed to the Google Groups "Blockly" group.
To unsubscribe from this group and stop receiving emails from it, send an email to blockly+u...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/blockly/CAGih2fTe0MzGB2JeAhW8gK%3DEFmf_Ea6GBoyvGsaggN47k8YjYg%40mail.gmail.com.


--

Neil Fraser

unread,
Jul 4, 2022, 11:48:02 AM7/4/22
to Blockly
The PR for resolving this is here:
Reply all
Reply to author
Forward
0 new messages