Scope modifiers: Difference between revisions
m
→{{header|Phix}}: added syntax colouring the hard way, phix/basics
Alextretyak (talk | contribs) (Added 11l) |
m (→{{header|Phix}}: added syntax colouring the hard way, phix/basics) |
||
Line 1,092:
=={{header|Phix}}==
{{libheader|Phix/basics}}
Identifiers are private by default, ie restricted to a single file, or they can be made global by prefixing the definition with the global keyword,
<!--<lang Phix>-->
<span style="color: #008080;">forward</span> <span style="color: #008080;">function</span> <span style="color: #000000;">localf</span><span style="color: #0000FF;">()</span> <span style="color: #000080;font-style:italic;">-- not normally necesssary, but will not harm</span>
<span style="color: #008080;">forward</span> <span style="color: #008080;">global</span> <span style="color: #008080;">function</span> <span style="color: #000000;">globalf</span><span style="color: #0000FF;">()</span> <span style="color: #000080;font-style:italic;">-- ""</span>
<span style="color: #008080;">function</span> <span style="color: #000000;">localf</span><span style="color: #0000FF;">()</span>
<span style="color: #008080;">return</span> <span style="color: #000000;">1</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
<span style="color: #008080;">global</span> <span style="color: #008080;">function</span> <span style="color: #000000;">globalf</span><span style="color: #0000FF;">()</span>
<span style="color: #008080;">return</span> <span style="color: #000000;">2</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
<!--</lang>-->
Here, localf() can only be invoked from within the same file, but globalf() can be invoked from any other file
that (directly or indirectly) includes it. The global keyword is equally applicable to routines, variables, and constants.
Line 1,110 ⟶ 1,114:
Namespaces for specific (entire) files can be used to qualify global identifiers, should there be a name clash between several files.
namespace xxx -- (only supported in Phix for compatibility with OpenEuphoria)▼
<!--<lang Phix>-->
<span style="color: #008080;">include</span> <span style="color: #000000;">somefile</span><span style="color: #0000FF;">.</span><span style="color: #000000;">e</span> <span style="color: #000000;">as</span> <span style="color: #000000;">xxx</span>
<span style="color: #000080;font-style:italic;">-- alternatively, within somefile.e:</span>
▲ <span style="color: #7060A8;">namespace</span> <span style="color: #000000;">xxx</span> <span style="color: #000080;font-style:italic;">-- (only supported in Phix for compatibility with OpenEuphoria)</span>
<span style="color: #000000;">res</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">xxx</span><span style="color: #0000FF;">:</span><span style="color: #000000;">globalf</span><span style="color: #0000FF;">()</span> <span style="color: #000080;font-style:italic;">-- call a global function named globalf, specifically the one declared in somefile.e</span>
<!--</lang>-->
Note however that one of the main reasons for namespaces is to avoid having to amend any included (3rd party)
files, so having namespaces within the file itself may prove to be less than helpful, should they (the namespaces
Line 1,122 ⟶ 1,130:
in and out of scope in every single source file throughout the application. What this means is that if file
a includes b includes c, you can refer in a to c via the namespace of b, but not directly, unless you also
explicitly include c in a.
The compiler maintains a list of files it has processed; re-inclusion (by some other file) just adds any
|