Defines a functional namespace.
Assigns namespaces to user-defined functions to disambiguate them.
Syntax
Code: Select all
namespace name
name the name of the namespace.
Must conform to the same rules as the names of functions:
can only consist of letters (a-z, A-Z), numbers, underscores, and must not start with a number.
Note that you cannot use any script variables to construct the name.
Quotes around the name are optional.
- functions under specific namespace are called by prepending the namespace to the function name separated by "::".
- namespace statements are terminated by linebreak, not by semicolon. In other words, namespace statements are not to be suffixed with a ";".
- namespace statements must be the only statement in their own line, not followed by any other statements nor comments.
- namespaces always have global scope, regardless of indentation.
- namespace can not be nested. Instead, a namespace inside another is elevated to global scope.
- There is a reserved namespace "xy" to refer to XY native functions. It can become useful when user-defined function names collide with native built-in functions. See examples.
Code: Select all
Example 1
~~~~~~~~~
Loaded script:
"Test sum"
echo sum(1, 2, 3); //3; calls the first sum function
"Test math::sum"
echo math::sum(1, 2, 3); //3; calls the first sum function in namespace "math"
"Test math2::sum"
echo math2::sum(1, 2, 3); //6; calls the first sum function in namespace "math2"
include "math.inc"
include "math2.inc"
Included library math.inc:
namespace "math"
function sum($x, $y) { return $x + $y; }
Included library math2.inc:
namespace "math2"
function sum($x, $y, $z) { return $x + $y + $z; }
Example 2
~~~~~~~~~
You can have more than one "namespace" statements in one resource:
Loaded script:
"Test sum"
echo sum(1, 2, 3); //3; calls the first sum function
"Test math::sum"
echo math::sum(1, 2, 3); //3; calls the first sum function in namespace "math"
"Test math2::sum"
echo math2::sum(1, 2, 3); //6; calls the first sum function in namespace "math2"
include "math3.inc"
Included library math3.inc:
namespace "math"
function sum($x, $y) { return $x + $y; }
namespace "math2"
function sum($x, $y, $z) { return $x + $y + $z; }
Example 3
~~~~~~~~~
There is a reserved namespace "xy" to refer to XY native functions:
echo xy::asc("a"); //97
Normally you can skip this, but it can become useful when your user
function names collide with native functions:
echo asc("a"); //ASC=97
echo xy::asc("a"); //97
function asc($a) { return "ASC=" . xy::asc($a) }
Example 4
~~~~~~~~~~
function embrace($arg) { return "($arg)" };
namespace curly
function embrace(arg) { return "{$arg}" };
namespace square
function embrace($arg) { return "[$arg]" };
namespace angle
function embrace($arg) { return "<$arg>" };
"test"
echo angle::embrace(square::embrace(embrace("tag")));
- v15.00.0523 - 2015-05-02 14:58: Scripting: Now include, function, and namespace statements can be indented.
- v15.00.0322 - 2015-04-14 16:41: the names of namespaces can be double- or single- or un-quoted, and must conform to the same rules as the names of functions.
- v15.00.0313 - 2015-04-10 16:40: Introduced.