QforMortals/workspace organization

From Kx Wiki
Jump to: navigation, search


Workspace Organization


The collection of entities that exist in a q session comprise the workspace. In other words, the workspace includes all atoms, lists, dictionaries, functions, enumerations, etc., that have been created through the console or via script execution.

As in any programming environment of reasonable complexity, there is the potential for name clashes. Should two separate scripts both create a variable called 'foobar', one will overwrite the value of the other. Here, typing in q is of no help, since a variable can be reassigned with a different type at any time.

The solution to name clashes is to create name spaces. This is often accomplished with a hierarchical naming structure implemented with dot notation in which two name spaces A and B can both have a entity foobar, but A.foobar and B.foobar are distinct. A familiar example of this is the hierarchical directory/file system used by operation systems.

Namespaces in q are called directories or contexts. Contexts provide an organization of the workspace.

Workspace Organization

The q workspace provudes a simple namespace structure using dot notation for entity names. Each of the nodes is called a context, or a directory. The default context, also called the root, comprises all entites whose names start with an initial alpha character. The variables we have created heretofor have resided in the default context.


A context name has the form of a dot (.) followed by alphnums, starting with an alpha. The following are all vallid context names.


There is no need to pre-declare the context name. As in the case of variables, a context is created dynamically as required. You specify a varaible to a context by prepending the context name to the varaible name, separated by a dot (.). The variable foobar can be created in various contexts,


Variables of the same name in different contexts are indeed distinct,


When an entity name includes its full context name, we say the name is fully qualified. When an entity name omits the context name, we say the name is unqualified.

Reserved Contexts

All contexts of a single letter (both lower and upper case) are reserved for q itself. Some of these are listed below:

Name Use
.q Built-in functions
.Q Low-level routines used by q
.z Environmental interaction
Warning.png While q will not prevent you from placing entities in the reserved contexts, doing so risks serious problems should you collide with names used by q.

Working with Contexts

At any time in q session, there is a current or working context. When you start a q session, the current context is the default context. You change the current context with the \d command. For example, to swicth to the 'files' context,

	\d .files

To switch back to the default context,

	\d .

To display the current context,


Any entity in the current context can be specified using its unqualified name.

	\d .                                         / switch to default contrext
	\d .files  home

A Context is a Dictionary

A context is actually a sorted dictionary whose domain is a list of symbols with the names of the entities defined in the context. Apply the key function to the dictionary name to display the names of the entities in the context. Apply value to see the entire dictionary mapping.

	.new.L:1 2 3
	.new.d:`a`b`c!1 2 3
	key `.new
	show value `.new
 | ::
a| 42
L| 1 2 3
d| `a`b`c!1 2 3

Observe that q places an entry into any non-default context for the null symbol.

You can look up an entity name in the directory to get its associated value. Use a symbol conataining the context name to refer to the dictionary.

1 2 3
Information.png In order to access an entity in the default context from another context, you must look up it's value. There is no syntactic form.
	\d .
	\d .new

Functions and Contexts

Function definition presents an issue with respect to global variable references and unqualified names. In the following function, the variable a is an unqualified global variable,


There is a potential ambiguity with respect to the context of a. Is the context resolved at the time f is defined, or is it resolved at the time f is evaluated?

Warning.png The context of an unqualfied global variable in a function is the context in which the function is defined, not the context in which it is evaluated.

Thus, we find

	\d .

	\d .lib
	\d .

We also find the following result, because even though g lives in the .lib context, it is defined in the default context.

	\d .
	\d .lib

Namespaces (Advanced)

It is possible to suimulate a multi-level namespace hierarchy by using multiple dots in names.


In the example above, q creates dictionaries at each node of the tree.

	value `.lib1.vars
	value `.math.fns

But appearances are deceiving. As of this writing (Oct 2006), q does not recognize a context tree below the first level. So, in our example, you can not switch to a context .lib1.vars using the \d command.

	\d .math.fns

You must access the contents of a node dictionary below the top level functionally.

	`.math.fns[`f] [6;7]

The following is arguably more readable,

6 7

Here is one way to perform late-bound computation using members in the context tree,

	mlib[`f] . vlib[`op1`op2]

Prev: I/O, Next: Commands

©2006 Kx Systems, Inc. and Continuux LLC. All rights reserved.

Personal tools