Send Feedback
Skip to content

Namespaces

Namespaces are containers within the KDB-X workspace. Names defined in a namespace are unique only within the namespace.

Namespaces are a convenient way to divide an application between modules; also to construct and share library code.

Namespaces are identified by a leading dot in their names.

System namespaces

KDB-X includes the following namespaces.

namespace contents
.h Functions for converting files into various formats and for web-console display
.j Functions for converting between JSON and q dictionaries
.m Module-local namespaces
.Q Utility functions
.q Definitions of q keywords
.z System variables and functions, and hooks for callbacks

The linked pages document some of the objects in these namespaces. (Undocumented objects are part of the namespace infrastructure and should not be used in KDB-X applications.)

These and all single-character namespaces are reserved for use by KX.

Names

Apart from the leading dot, namespace names follow the same rules as names for q objects.

Outside its containing namespace, an object is known by the full name of its containing namespace followed by a dot and its own name.

Namespaces can contain other namespaces.

Thus .fee.fi.fo is the name of object fo within namespace fi within namespace fee.

Dictionaries

Namespaces are implemented as dictionaries. To list the objects contained in namespace .foo:

key `.foo

To list all the namespaces in the root:

key `

The contents of all namespaces may be retrieved using value`. The default namespace is represented by a null symbol.

q)a:1
q)value`
 | (,`a)!,1
z| ``ph!(::;k){gz:#ss[x[1]`$"Accept-Encoding";"gzip"];x:uh$[@x;x;*x];tf:$[2=."\\e";{.Q.trp[y;z;{he y,"\n",.Q.sbt..
q| ``neg`not`null`string`reciprocal`floor`ceiling`signum`mod`xbar`xlog`and`or`each`scan`over`prior`mmu`lsq`inv`m..
Q| ``ajf0`sx`k`K`host`addr`gc`ts`gz`tel`lim`w`res`addmonths`f`fmt`ff`fl`opt`def`ld`qO`qt`v`qp`V`ft`ord`nv`tx`tt`..
h| ``htc`hta`htac`ha`hb`pre`xmp`d`cd`td`hc`xs`xd`ex`iso8601`eb`es`ed`edsn`ec`tx`xt`ka`c0`c1`logo`sa`html`sb`fram..
j| ``e`q`s`es`J`k`jd`j!(::;@[k){"c"$$[x<128;x;0b/:'@[;0;(1b,i#1b),]10b,/:((5 10;0 4 10)i:x>2047h)_0b\:"h"$x]}]25..
s| ``init!(::;k){"s"" ";})

Construction

Referring to a namespace is sufficient to create it.

q)key `
`q`Q`h`j`o
q).fee.fi.fo:42
q)key `
`q`Q`h`j`o`fee
q)key `.fee
``fi
q)key `.fee.fi
``fo

Current namespace

The q interpreter has a current namespace, with the default namespace being .. The current namespace can be changed using the \d system command. Any variables without a namespace prefix are treated as being in the current namespace.

q)\d .foo
q.foo)f:{x+1}  // this is .foo.f
q.foo)g:{x*2}  // this is .foo.g

Global variable references in functions retain the current namespace the function was defined in, as opposed to changing to whichever namespace is current when the function is called:

q)\d .foo
q.foo)MULT:3
q.foo)f:{x*MULT}
q.foo)\d .
q)MULT:4
q).foo.f 1
3

A function can be created with an overridden current namespace using the syntax q.ns), which is similar to what the prompt looks like after switching the current namespace:

q).foo.MULT:5
q)q.foo)f:{x*MULT}
q).foo.f[1]
5

During the loading of a module, the current namespace is switched to a namespace local to the module.


\d

Q for Mortals §12 Workspace Organization