microsoft / microsoft/pxt-microbit
Clarification about for loop index variables
Nobody has claimed this yet.
- Dominant language
- TypeScript
- Stars
- 804
- Forks
- 721
- Avg merge
- 2d 9h
- Merged PRs (30d)
- 2
Description
Describe the bug
Arising from support tickets (private):
https://support.microbit.org/helpdesk/tickets/88776
https://support.microbit.org/helpdesk/tickets/88841
Changing modes sometimes results in extra numbered variables being created, particularly with for loops.
For example, when there are two for loops, a round trip to JavaScript with a minor edit, changes the second index to index2, though it doesn't really need to be.
https://makecode.microbit.org/_3ALKHjemTc9L
Another case is when the same name appears both inside and outside a for loop.
- An existing variable can be selected as the index of a for loop https://makecode.microbit.org/_HAP1Ufe50feR
- Conversely, when a for loop is created, the index variable's name appears in toolbox/Variables, and can be dragged outside the loop. https://makecode.microbit.org/_5k5aAxPRPAdM
The behaviour is not obvious - it's as if there are two variables with the same name. It's clear in JavaScript mode that the loop index is a local that shadows the global inside the loop. It's less obvious in Python.
There is a subtle difference in appearance between variable blocks originating from the index variable and those created from toolbox/Variables. The loop index form disappears after a round trip with a minor edit in JavaScript mode. See both forms inside and outside the loop in this example.
https://makecode.microbit.org/_HAP1Ufe50feR
A round trip to Python can result in a for being replaced with a while.
Could the shadowed global be avoided - "that name already exists"?
Should the numbering happen sooner?
Should it be explained in documentation? https://makecode.microbit.org/blocks/loops/for
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Reproduce the linked examples in Blocks, JavaScript, and Python modes, focusing on for-loop index variables, shadowed globals, and round trips. Compare the behavior with the linked for-loop documentation; completion depends on an agreed resolution for naming, scope, conversion, and documentation.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- typescript
- Domain
- frontend
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Needs clarification
- Newbie friendliness
- 35/100