<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN"> <HTML> <HEAD> <META NAME="GENERATOR" CONTENT="SGML-Tools 1.0.9"> <TITLE> A Guide to the S-Lang Language: Name Spaces</TITLE> <LINK HREF="slang-11.html" REL=next> <LINK HREF="slang-9.html" REL=previous> <LINK HREF="slang.html#toc10" REL=contents> </HEAD> <BODY> <A HREF="slang-11.html">Next</A> <A HREF="slang-9.html">Previous</A> <A HREF="slang.html#toc10">Contents</A> <HR> <H2><A NAME="s10">10. Name Spaces</A></H2> <P> <P>By default, all global variables and functions are defined in the global namespace. In addition to the global namespace, every compilation unit (e.g., a file containing <B>S-Lang</B> code) has an anonymous namespace. Objects may be defined in the anonymous namespace via the <CODE>static</CODE> declaration keyword. For example, <BLOCKQUOTE><CODE> <PRE> static variable x; static define hello () { message ("hello"); } </PRE> </CODE></BLOCKQUOTE> defines a variable <CODE>x</CODE> and a function <CODE>hello</CODE> in the anonymous namespace. This is useful when one wants to define functions and variables that are only to be used within the file, or more precisely the compilation unit, that defines them. <P>The <CODE>implements</CODE> function may be used to give the anonymous namespace a name to allow access to its objects from outside the compilation unit that defines them. For example, <BLOCKQUOTE><CODE> <PRE> implements ("foo"); static variable x; </PRE> </CODE></BLOCKQUOTE> allows the variable <CODE>x</CODE> to be accessed via <CODE>foo->x</CODE>, e.g., <BLOCKQUOTE><CODE> <PRE> if (foo->x == 1) foo->x = 2; </PRE> </CODE></BLOCKQUOTE> <P>The <CODE>implements</CODE> function does more than simply giving the anonymous namespace a name. It also changes the default variable and function declaration mode from <CODE>public</CODE> to <CODE>static</CODE>. That is, <BLOCKQUOTE><CODE> <PRE> implements ("foo"); variable x; </PRE> </CODE></BLOCKQUOTE> and <BLOCKQUOTE><CODE> <PRE> implements ("foo"); static variable x; </PRE> </CODE></BLOCKQUOTE> are equivalent. Then to create a public object within the namespace, one must explicitly use the <CODE>public</CODE> keyword. <P>Finally, the <CODE>private</CODE> keyword may be used to create an object that is truly private within the compilation unit. For example, <BLOCKQUOTE><CODE> <PRE> implements ("foo"); variable x; private variable y; </PRE> </CODE></BLOCKQUOTE> allows <CODE>x</CODE> to be accessed from outside the namespace via <CODE>foo->x</CODE>, however <CODE>y</CODE> cannot be accessed. <P> <P> <HR> <A HREF="slang-11.html">Next</A> <A HREF="slang-9.html">Previous</A> <A HREF="slang.html#toc10">Contents</A> </BODY> </HTML>