A namespace is a collection of unique names and corresponding values. You can store references to namespaces in variables. Namespace names are global, because JMP has only one namespace map. Namespace references are variables like any other variable that references an object, so they must be unique within their scope or namespace. The members of a namespace are referenced with the : scoping operator, such as my_namespace:x to refer to the object that is named x within the namespace called my_namespace. See User-Defined Namespace Functions for details about creating and managing your own namespaces. Namespaces are especially useful for avoiding name collisions between different scripts.
nsref = New Namespace( <"nsname">, <{ name = expr, ... }> );
Creates a new namespace called nsname (a string expression) and returns a reference to the namespace. All arguments are optional.
Nsname is the name of the namespace in the internal global list of namespace names. Nsname can be used as the prefix in a scoped variable. The function returns a reference to the namespace, and can also be used as the prefix in a scoped variable reference. If nsname is absent, the namespace is anonymous and is given a unique name created by JMP. Show Namespace() shows all namespaces and their names, whether assigned or anonymous.
Important: If you already have a namespace named nsname, it is replaced. This behavior means that while you are developing your script, you can make your changes and re-run the script without having to clear or delete your namespace. To avoid unintentional replacement, you can either use anonymous namespaces, or test to see whether a particular namespace already exists:
If( !Namespace Exists( "nsname" ), New Namespace( "nsname" ) ); |
nsref = Namespace( "nsname" | nsref);
Note: Namespace() returns a reference to a namespace that already exists. It does not create a new namespace.
b = Is Namespace( nsref );
b = As Scoped( "nsname", var_name);
nsname:var_name;
As Scoped() is the function form of a scoped reference. The function returns a reference to the specified variable in the specified scope.
b = Namespace Exists( "nsname" );
Show Namespaces();
Shows the contents of all namespaces contained in the list of global namespaces. Namespaces are not visible unless a reference is made to one, using either the New Namespace or Namespace functions.
Note that these messages, as with all message, must be sent to a scriptable object. A namespace name is not a defined scriptable object and cannot be used in a Send operation. However, you can use the name of a namespace in variable references. For example, nsmane::var is equivalent to nsref::var.
Namespace Messages defines the messages that are supported by user-defined namespace references.
Returns 1 or 0, depending on whether var_name exists within the namespace.
|
|
Returns the unevaluated expression that var_name contains in this namespace.
|
|
Locks all variables in the namespace and prevents variables from being added or removed. <<Unlock unlocks all of the namespace’s variables.
|
|
The following are all equivalent references to a variable that is named b in the namespace that is named nsname that has a reference nsref:
nsref:b
nsname:b
"nsname":b
nsref["b"]
nsref<<Get Value("b") // used as an r-value
•
|
use anonymous names created by the New Namespace function
|
There is also an option for the Include function (New Context) that creates a namespace that the included script runs in. This namespace is anonymous and it is independent from the parent script’s namespace. For example:
Include("file.jsl", <<New Context);
If both the parent and included scripts use the global namespace, add Names Default To Here to the New Context option to avoid name collisions. For example:
Include("file.jsl", <<New Context, <<Names Default To Here);
new_emp = New Namespace(
{name_string = "Hello, *NAME*!",
print_greeting = Function( {a},
Print( Substitute( new_emp:name_string, "*NAME*", Char( a ) ) )
)}
);
new_emp:print_greeting( 6 );
"Hello, 6!"
If( !Namespace Exists( "complex" ),
New Namespace(
"complex"
);
complex:makec = Function( {a, b},
Eval List( {a, b} )
);
complex:addc = Function( {a, b}, a + b );
complex:subtractc = Function( {a, b}, a - b );
complex:multiplyc = Function( {a, b},
Eval List( {a[1] :* b[1] - a[2] :* b[2], a[1] :* b[2] + a[2] :* b[1]} )
);
complex:dividec = Function( {a, b},
d = b[1] ^ 2 + b[2] ^ 2;
Eval List(
{a[1] :* b[1] - a[2] :* b[2] / d, a[2] :* b[1] - a[1] :* b[2] / d}
);
);
complex:charc = Function( {a},
Char( a[1] ) || "+" || Char( a[2] ) || "i"
);
);
Namespace( "complex" ) << Lock;
c1 = complex:makec( 3, 4 );
c2 = complex:makec( 5, 6 );
cadd = complex:addc( c1, c2 ); // returns {8, 10}
csum = complex:subtractc( c1, c2 ); // returns {-2, -2}
cmul = complex:multiplyc( c1, c2 ); // returns {-9, 38}
cdiv = complex:dividec( c1, c2 ); // returns {14.6065573770492, 19.7049180327869}
Show( complex:charc( c1 ) ); // returns complex:char(c1) = "3+4i";
cm1 = complex:makec( [1, 2, 3], [4, 5, 6] ); // returns {[1, 2, 3], [4, 5, 6]}