pxt icon indicating copy to clipboard operation
pxt copied to clipboard

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

Open abchatra opened this issue 7 years ago • 1 comments

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.

abchatra avatar Mar 14 '18 22:03 abchatra

I have experienced the same kind of issue.

Possible solution: Make locally scoped variables an "advanced" block feature and have the presence of features like this force the advanced block editor.

Global enhancement request: Add advanced programming structuers and techniques to the advanced editor to allow the programmer to try advanced techniques before abandoning the block editor completely.

jharris1993 avatar Jan 15 '19 10:01 jharris1993