scope of let

25 views
Skip to first unread message

Claus Möbus

unread,
Jan 4, 2020, 12:45:21 PM1/4/20
to webppl-dev
Dear all,
happy and successful New Year to you all. 
Now, to WebPPL. To my astonishment I saw two lets in ch.07 of the ebook Cognition models. More precisely it was in a code box explaining the MH-algorithm.
I always saw that "var" was used for the purpose of definitions. "let" was not in the WebPPL docs so I looked how "let" was defined in JS. 

I read that "var" has no block scope but "let" has since ES2015. I tried an example from W3School. This revealed that "let" in WebPPL has **no** block scope:

Here's the code snippet:

var x = 10;
// Here x is 10
print("x1 = " + x)
{
  let x = 2;
  // Here x is 2
  print("x2 = " + x)
}
// Here x is 10
print("x3 = " + x)

WebPPL's output is:

x1 = 10
x2 = 2
x3 = 2
it should be:
x1 = 10
x2 = 2
x3 = 10
Best, Claus

null-a

unread,
Jan 5, 2020, 4:08:22 AM1/5/20
to webppl-dev
Hello. Happy new year to you too.

The way I see it, this is a bug, in the sense that (IIUC) the language was never intended to include `let`, so using it should be a syntax error.


(I suspect this happens because the parser is doing something like the following: Both `var` and `let` are represented as similar "variable declaration" nodes in the AST, and are only distinguished by a flag on the node, which WebPPL is ignoring.)

Thanks,
Paul
Reply all
Reply to author
Forward
0 new messages