What is an upvar?

I have a basic idea of what they are but how can I apply one to a custom block, and if I did so how would it be used?
Also sorry if this is in the wrong place..

its not.

Screen Shot 2021-02-05 at 5

An upvar is the variable in the "for to" primitive
image
You can drag it out and read and modify the variable. The code in the block itself can also do that.

Sorry I misworded it, I meant how is it generally used in a custom block?

It's used in blocks (custom or primitive) that take a script as an input (i.e., C-shaped blocks) and want to provide information to that script. It's sort of an output from the block rather than an input to it. Does that help?

Yep, thanks! I'll test it out in a little while.

yes,

. however, when you choose a script as an input, you should only use the upvar in the script.

Isn't it more general than that. My favourite use of upvar is

image

And on the topic of upvar is there anything similar in other programming languages?

Yeah, that has a certain bizarre elegance, and Jens loves that upvars work like script vars in the outer script. But I hate it, partly because it's messing up my attempt to do Church numerals using exactly lambda calculus notation, but also, I find myself surprisingly conservative about some programming language issues. The question of whether the index variable in a FOR loop should survive (with the value that caused the loop to finish) goes all the way back to arguments about the Fortran DO loop's variable. I think of that variable as belonging inside the loop.

I thought not for a long time, and I'm pretty sure we're the only block language with such a feature, but a while back I realized that call by reference is essentially the same feature, in text language form.

Interesting.

Regarding for loops in JavaScript one can have

for (let i = 0; ... // i is local to the for loop

or

for (var i = 0; ... // i is scoped to the function

And it seems people argue over which is better.

I too prefer the scope to be the for loop.

Does that just initialize a function scoped (or whatever that's called) variable named var?
edit: tested it out and it appears so. Cool application. However can't you already do that with the script variable block?

You can do it with a SCRIPT VARIABLES followed by a SET. This is just an abbreviation.

Yes, upvar declares an input as a variable that can be set like call by reference but that is only half of the role of upvar. upvar also creates/declares a variable in the outer scope, unlike call by reference where the variable has to be declared elsewhere.

True.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.