How to nest gets from Local Storage?

Sample app: two TextInput fields, both of whose contents are written to Local Storage (call them “first input” and “second input”). Now you want to pull out the two values and add them together.

The code that we wrote is as follows. I don’t have the ability to paste an image here for complicated reasons, so I’ll write in a textual equivalent:

when Button3 Click do:
  in Local_Storage1 call Get:
    key "first input"
    with output <green value>
    then do:
      in Local_Storage1 call Get:
        key "second input"
        with output <green value>
        then do:
          from TextInput4 set Text to:
            <green value> + <green value>

where is the green block named value that is supplied by default by LocalStorage.

Note that it seems confusing that both values are the same color. That’ll be an issue soon!

Nevertheless, we very carefully dragged the first green value to the left of + and the second green value to the right of +.

Despite this, the output is always twice the second input. That is, the second green value masks the first green value.

In other words, the two values don’t have individual identities. Rather, they have only their name, and the second name masks the scope of the first one. Really, each value ought to be distinct, and its distinctiveness would ideally be represented by a difference in color or shade (so you’d know which value you dragged where).

I tried right-clicking on the value block to see whether it offered a way to alpha-rename, but it does not appear to (unless I missed something?).

So I’m not entirely sure how to suggest my daughter rewrite the above program.

Thanks for any help!

Broader Notes:

  • If the getter would just return a value, then there would be no need for any of this: the two arguments to + would be the two calls to the getter.

  • If the getter would take a variable as the place to deliver its output, we could create two variables, but it doesn’t. We created a variable and tried to drag it in place of value. It appear to slot in and displace value, but there was some weirdness on screen. The weirdness grew much greater once we dragged the outer block: the variable’s block left shadow trails all across the screen. It was a psychedelic sight to behold!

  • This extreme-sequential programming interface for Local Storage is really ugly. It’s like the worst case of continuation-passing style: all the clunky sequentiality without even the benefit of fresh names!

  • If the interface hangs around, value ought to be treated hygienically (in the Scheme macro sense).

Having getters return values would completely clean up the above code, of course, and avoid the last three items above. However, the lack of hygiene may affect other components that must follow this extreme-imperativity pattern, so this probably needs a broader look.

You could just create variables and assign value to the variable before the second call. Something like:

28

Regards Rob

1 Like

Thanks for the helpful response.

I was not quite clear enough in my question: I could indeed, but my issue is at least partly pedagogic. I don’t think there’s any way to have expected my kid to have thought of that, or even to necessarily understand why that’s the right thing to do. Given that the documentation is silent on this, I’m wondering how others who use Thunkable deal with this very natural situation, which surely shows up a lot.