What is the 'block variable' really good for?

In general and from other programming languages I am quite familiar with scope concepts for variables like 'local' or 'global'. Then I stumbled across the 'block variable' in Snap! which seems to me like a hybrid of local and global.
Does anyone know a situation, where a block variable is a better solution than a local or global variable?

How is it hybrid with global?

I don't actually know what the scope is, whether it is available only in the current custom block/script, or if it is also available 'lower' (inside called blocks) or not, but I'm pretty sure it is not available 'higher'

Block variables are variables that can be stored in a specific block, and they do not get shared across the other custom blocks. For example, this block will have the same value when called, but each instance will be a different value.

You should try this out.

They're persistent local variables. That is, they have the same scope as a script variable, but infinite extent.

The thing that's a little weird about block variables (apart from their name, which I find mysterious) is that each actual visible copy of the block has its own variable. That is, when you drag (a copy of) the block out of the palette into the scripting area, you are thereby making a new block variable. But if you call that copy repeatedly, the calls all have the same block variable. (Similarly, using the Duplicate option of the block's context menu makes a new block variable to go with the new block instance.)

The problem with this syntax is that there's no way to give the block variable an initial value other than 0. The more general way to create a persistent local state variable is this:

untitled script pic (2)
untitled script pic (4)
untitled script pic (5)
PLUS3 and PLUS5 have separate variables NUM, created when MAKE-ADDER is called.

Wow! As a Snap! beginner your example really overwhelmed me and it took me quite some time to understand what it does. I had no clue, that it is possible in Snap! to dynamically generate new functions and I have never seen something like this in other programmimg languages (or I just ignored it because it seemed to exotic to me).
What the block variable is concerned, I'm afraid I still cannot see any advantage of having a language construct like that.But probably I am just missing a kind of higher insight.

I did. And I learned that the block variable is invisible, not only to other custom blocks but also to the calling blocks. So it seems to be just a vampire variant of the script variable, which is unable to die.

You are right. The term 'hybrid' was indeed not veriy appropriate. And a block variable is definitely not accessible by the calling block or any other block.


Here's the paradigmatic example:
untitled script pic
Drag a couple of copies of this block out of the palette, click on one of them a few times, then click on the other, and switch back and forth. Each copy has its own count.

Actually, pretty much every programming language except Python supports lambda these days. That's what the gray rings are.

Welcome to Snap!

Yes, Python has this thing they call lambda that can only be one line long, can't be a reporter, and has incorrect scope rules.

All the other languages have real, lexically scoped, unrestricted lambda.

It has to be an expression, which is pretty much a reporter. I think you mean "can't be a command".

Oh yeah, right. I knew it was one of those. ;~/

oh, "BLOCK" variables, I thought you were just talking about "SCRIPT" variables. Thx everybody for the clarifying discussion.

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