microsoft / microsoft/pxt

Round trip to blocks and back causes semantic differences for local scoped variables

Open
#3,979 1 comment 0 reactions 1 assignee View on GitHub

@riknoll is already working on this.

Since Mar 14, 2018.

bug decompiler P2
Dominant language
TypeScript
Stars
2.3k
Forks
641
Avg merge
12h 4m
Merged PRs (30d)
57

Description

Copy past the following program to typescript

function gameOfLife() {
    for (let b = 0; b <= 4; b++) {
        let count = 0
        basic.showNumber(count + b)
        count += 1
    }
}
gameOfLife()

Output: 0,1,2,3,4

Convert to blocks and back to typescript.

let count = 0
function gameOfLife() {
    for (let b = 0; b <= 4; b++) {
        basic.showNumber(count + b)
        count += 1
    }
}
gameOfLife()

Now the count variable is hoisted and output is different (0,2,4,6,8). This is due to fact that set to count is not reset.

count is treated as global variable in typescript. But we shouldn't get rid of set at the local scope.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.