Replies: 1 comment 1 reply
-
Glad you found an acceptable alternative. Another way you can hide variables from other cells is to use a block statement to make them local (non-top-level) variables; however, this can prevent you from using let outer;
{
function inner() { ... }
outer = function outer() { inner(...) };
} I’ve been thinking |
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Edit: I suppose I could just use an IIFE and that might be a clearer expression of the same idea... 💡. Closing!
I've been working with some increasingly sophisticated notebooks and had a random idea for a way to make reasoning about them a little bit simpler. The basic idea is to add support for the
export
keyword in a cell to restrict which variables are accessible from outside the cell.Background
With the ability for a cell to define multiple values, I'm noticing myself co-locating a variable or function with its supporting variables or functions.
For example:
where the
outer
function is referenced from outside the cell, butinner
is not.Or
where
computed
is used outside of this cell, butres
andraw
are not.The idea
Right now, all of the variables defined inside of a cell are accessible from the outside, but I'm wondering if it could make sense to allow specifying certain variables as
export
ed to restrict a cell to exposing only those variables as outside references.This would ease the understanding of complex notebooks (since you can tell which values are or aren't exported) and would also ease the path towards abstracting a cell into an externally-imported JavaScript file since you can centralize the library-to-be in a cell before moving the code to a file.
With this proposal, the code above would look like
and
With the above, only the
outer
andcomputed
variables would be accessible outside of their defining cell.In a cell with no explicit exports, all variables would be treated as exported.
Downsides
One downside of this proposal would be that it is no longer obvious by looking purely at a variable definition whether or not that variable is accessible from other cells. I don't know if this would be a problem in practice, and if it is then one way to potentially address it would be to use syntax coloring to show the "cell-private" names differently from the exported names.
I'm not sure if this one is an upside or a downside, but this idea feels strongly linked to a compilation strategy that treats each cell as an independent JS module.
Beta Was this translation helpful? Give feedback.
All reactions