Scope modifiers: Difference between revisions
Content added Content deleted
Alextretyak (talk | contribs) (Added 11l) |
m (→{{header|Phix}}: added syntax colouring the hard way, phix/basics) |
||
Line 1,092: | Line 1,092: | ||
=={{header|Phix}}== |
=={{header|Phix}}== |
||
{{libheader|Phix/basics}} |
|||
Identifiers are private (restricted to a single file) by default, or they can be made global by prefixing the |
|||
definition with the global keyword |
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, outside of routines only - everything declared inside a routine is always private to that routine. Should a forward declaraion of a routine exist, it must match the actual definition in terms of presence/absence of a global prefix, as well as parameter types, etc. |
||
<lang Phix>forward function localf() -- not normally necesssary, but will not harm |
|||
forward global function globalf() -- "" |
|||
<!--<lang Phix>--> |
|||
function localf() |
|||
<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> |
|||
return 1 |
|||
<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> |
|||
end function |
|||
<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>--> |
|||
global function globalf() |
|||
return 2 |
|||
end function</lang> |
|||
Here, localf() can only be invoked from within the same file, but globalf() can be invoked from any other file |
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. |
that (directly or indirectly) includes it. The global keyword is equally applicable to routines, variables, and constants. |
||
Line 1,110: | Line 1,114: | ||
Namespaces for specific (entire) files can be used to qualify global identifiers, should there be a name clash between several files. |
Namespaces for specific (entire) files can be used to qualify global identifiers, should there be a name clash between several files. |
||
<lang Phix>include somefile.e as xxx |
|||
-- alternatively, within somefile.e: |
|||
⚫ | |||
<!--<lang Phix>--> |
|||
res = xxx:globalf() -- call a global function named globalf, specifically the one declared in somefile.e</lang> |
|||
<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: #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) |
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 |
files, so having namespaces within the file itself may prove to be less than helpful, should they (the namespaces |
||
Line 1,122: | Line 1,130: | ||
in and out of scope in every single source file throughout the application. What this means is that if file |
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 |
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. |
explicitly include c in a. Obviously were something in c globally unique you could refer to it without any namespace. |
||
The compiler maintains a list of files it has processed; re-inclusion (by some other file) just adds any |
The compiler maintains a list of files it has processed; re-inclusion (by some other file) just adds any |