Perhaps an opinion, but I think this is one of those cases where, if you want to go looking for differences, you can find them, but it might not be a meaningful exercise.
There is one difference here that is meaningful: ~something
vs x
.
x
is an interpreter variable. This is declared in Interpreter (and, as noted above, initialized to nil
):
Interpreter {
// The interpreter defines a context in which interactive commands
// are compiled.
var <>cmdLine; // place holder for text executed from a worksheet
var context; // faked interpreter context frame. Don't mess with it.
// a-z are predefined variables for use by the interactive context.
// They are read+write so that programmatic methods can
// get and alter the values that the interpreter has access to.
var <>a, <>b, <>c, <>d, <>e, <>f, <>g, <>h, <>i, <>j;
var <>k, <>l, <>m, <>n, <>o, <>p, <>q, <>r, <>s, <>t;
var <>u, <>v, <>w, <>x, <>y, <>z;
...
}
So if you just open SC and run x
, it will look up the variable declared here and return the auto-initialized value nil
.
~something
(i.e. any environment variable) is a member of a collection, the currentEnvironment
. When you first start SC, the current environment is topEnvironment
, and this environment is empty.
In SC, if you look up a collection entry by name or index and that name/index doesn’t exist, the result is nil
. (For arrays, some other programming languages would throw an “index out of bounds” error in that case, but SC doesn’t.)
So in your specified case (first thing to run in SC is ~something
), the topEnvironment collection is empty, and remains empty, and you try to look up \something
in it, and it’s not found, and the result is nil
.
If you do ~something = nil
, then it will try to .put
nil in under \something. This will fall through to the line value ?? { this.removeAt(key); ^this };
– if the value is nil, remove the key. But the key doesn’t exist, so removeAt
simply returns without altering the collection. That is, there is no “something” that gets created and then deleted. It didn’t exist initially; it doesn’t exist at the end; and it doesn’t exist anywhere in the middle either.
This might be confusing WRT declaration or definition – but, remember that environment variables are members of a collection. They are not and were never variables, so there is no “declaring” them. ~something
is only a shortcut syntax for \something.envirGet
. Using environment variables is only manipulating the collection – putting things into it, getting things out of it – no declaration needed.
hjh