Sophie

Sophie

distrib > Mandriva > 8.1 > i586 > by-pkgid > 700475c8ae73fb4d57b6df4485c29e1c > files > 170

slang-doc-1.4.4-2mdk.i586.rpm

<!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-&gt;x</CODE>, e.g.,
<BLOCKQUOTE><CODE>
<PRE>
     if (foo-&gt;x == 1) foo-&gt;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-&gt;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>