Check for existence of mutable var

I have a function in a cell that updates another mutable cell/variable. Is there any way to check if the mutable cell exists?

currently, if I have

somecell = { 

mutable foo = bar;
}

but haven’t defined mutable foo in another cell, I get:
RuntimeError: mutable foo is not defined.

I was wondering if I could do something like this:

somecell = { 
if (foo) { mutable foo = bar;}
}

Thank you!

Nope! All cells must be declared statically so that the runtime can compute the dependencies. That said, you can define the initial value of a mutable as undefined:

mutable foo = undefined

Then it’s up to you when you set the value to something else.

Thanks for the reply @mbostock.

What I’m trying to do is have be able to run the function with a “fallback” in case it’s used without setting up the external mutable var cell… thus why I’m trying to check for its existence.

Is there any way you recommend I can achieve this?

Definitely!

As Mike said: When you create your mutable, set it initially to your “fallback” value.

mutable foo = fallbackValue

That value could be undefined, false, null, or whatever makes sense for your code.

Then, when you’re ready, set the mutable to the live, actual value:

{
  if (ready) {
    mutable foo = bar;
  }
}

Any cell that references foo, will be reevaluated automatically, as soon as the value changes from the fallback value to bar.

Does that answer your question?

@jashkenas, Thanks for your reply.

Here is the code I’m working with:

The goal is to make the “angle” cell a reusable function that I can import into a different notebook.

There are two scenarios then:

  1. We use an external “mutable” var cell to track the current angle. In this case we setup a mutable var cell called currentTheta as demonstrated in the above notebook. In the handleMouse function, we update the currentTheta var. We also use it to set the initialAngle var.
  2. The other case is the “fallback”. Let’s say I import the angle cell into a different notebook, but I don’t setup a currentTheta mutable var cell. Currently, I get a runtime error that says the currentTheta variable is undefined, since… it is! What I’d like to do is be able to check to see if the currentTheta exists first before trying to set it. Unfortunately the check itself seems to give a runtime error as well. You can see this by getting rid of the currentTheta cell in the above notebook.

Hopefully this helps clarify what I’m looking for.

So @jashkenas, it’s not that I’m trying to declare the fallback value for the mutable foo , it’s that I’m trying to conditionally opt out of having a dependency on the external foo var cell at all in my function.

Thanks again for your help!

Gotcha.

In the case where you’d like the API to be a single reusable cell that you can import into different notebooks, then I think you just want one value — not a separate mutable one to control the cell. So try something like this:

The change I’ve made here is to make angle a function that returns and Observable view — that is to say, a piece of DOM (the svg), that also has a .value property (the current angle).

Now, display at the top becomes viewof display (the DOM), and the display value is the currentTheta. No need for mutable at all.

You can then initialize the initial angle as you see fit in the angle function, read it as a reactive value, and also mutate it from the outside. For more info, see the documentation on how viewof works:

Thanks @jashkenas. I tried this approach before as well, and it solves getting the value of the current angle OUT of the angle function for use in different parts of the notebook, but the main UX goal was to avoid resetting the visualization when the angle function gets re-evaluated.

So for example, when you use the “Angle Options” in the controls cell to toggle the display of the beta angle, the angle function gets re-evaluated and resets the start angle. When I had the currentTheta as an external (mutable) cell var, it could function as a place to store the visualization state for this re-evaluation. This is really the crux of why I was trying to use the external mutable var cell.

Any thoughts on how I can preserve the state with this approach? I can get this to work unidirectional, but it’s not clear to me how to get the mouse position to update the manualAngle or “also mutate it from the outside” without causing a circular dependency.

@digitaltopo I feel you are running into some limitations of the mutable operator (intentional limitations for the sake of simplicity), and that you’d be better off diving deeper into custom views and viewof. Here’s a new tutorial for you: