Scope modifiers: Difference between revisions
Content deleted Content added
→TI-89 BASIC: new example |
m Fixed lang tags. |
||
Line 6: | Line 6: | ||
===Public and private declarative parts=== |
===Public and private declarative parts=== |
||
In [[Ada]] declarative region of a package has publicly visible and private parts. The private part is introduced by '''private''': |
In [[Ada]] declarative region of a package has publicly visible and private parts. The private part is introduced by '''private''': |
||
<lang ada> |
<lang ada>package P is |
||
package P is |
|||
... -- Declarations placed here are publicly visible |
... -- Declarations placed here are publicly visible |
||
private |
private |
||
... -- These declarations are visible only to the children of P |
... -- These declarations are visible only to the children of P |
||
end P; |
end P;</lang> |
||
</lang> |
|||
Correspondingly a type or object declaration may be incomplete in the public part providing an official interface. For example: |
Correspondingly a type or object declaration may be incomplete in the public part providing an official interface. For example: |
||
<lang ada> |
<lang ada>package P is |
||
package P is |
|||
type T is private; -- No components visible |
type T is private; -- No components visible |
||
procedure F (X : in out T); -- The only visible operation |
procedure F (X : in out T); -- The only visible operation |
||
Line 25: | Line 22: | ||
procedure V (X : in out T); -- Operation used only by children |
procedure V (X : in out T); -- Operation used only by children |
||
N : constant T := (Component => 0); -- Constant implementation |
N : constant T := (Component => 0); -- Constant implementation |
||
end P; |
end P;</lang> |
||
</lang> |
|||
===Bodies (invisible declarations)=== |
===Bodies (invisible declarations)=== |
||
The keyword '''body''' applied to the packages, protected objects and tasks. It specifies an implementation of the corresponding entity invisible from anywhere else: |
The keyword '''body''' applied to the packages, protected objects and tasks. It specifies an implementation of the corresponding entity invisible from anywhere else: |
||
<lang ada> |
<lang ada>package body P is |
||
package body P is |
|||
-- The implementation of P, invisible to anybody |
-- The implementation of P, invisible to anybody |
||
procedure W (X : in out T); -- Operation used only internally |
procedure W (X : in out T); -- Operation used only internally |
||
end P; |
end P;</lang> |
||
</lang> |
|||
===Private children=== |
===Private children=== |
||
The keyword '''private''' can be applied to the whole package, a child of another package: |
The keyword '''private''' can be applied to the whole package, a child of another package: |
||
<lang ada> |
<lang ada>private package P.Q is |
||
private package P.Q is |
|||
... -- Visible to the siblings only |
... -- Visible to the siblings only |
||
private |
private |
||
... -- Visible to the children only |
... -- Visible to the children only |
||
end P.Q; |
end P.Q;</lang> |
||
</lang> |
|||
This package can be then used only by private siblings of the same parent P. |
This package can be then used only by private siblings of the same parent P. |
||
=={{header|AutoHotkey}}== |
=={{header|AutoHotkey}}== |
||
Line 279: | Line 271: | ||
scoped_global scope gives x = From global scope |
scoped_global scope gives x = From global scope |
||
scoped_notdefinedlocally scope gives x = From global scope |
scoped_notdefinedlocally scope gives x = From global scope |
||
>>> |
>>></lang> |
||
More information on the scope modifiers can be found [http://docs.python.org/3.0/reference/simple_stmts.html#grammar-token-global_stmt here].<br> |
More information on the scope modifiers can be found [http://docs.python.org/3.0/reference/simple_stmts.html#grammar-token-global_stmt here].<br> |
||
=={{header|R}}== |
=={{header|R}}== |
||
R uses lexical scoping – there is a nice introduction to this in the [http://cran.r-project.org/doc/FAQ/R-FAQ.html#Lexical-scoping FAQ on R]. There are no keywords related to scoping, but the [http://stat.ethz.ch/R-manual/R-patched/library/base/html/assign.html assign] function allows you to choose the environment in which a variable is assigned. The global assignment operators , '<<-' and '->>' are effectively shorthand for 'assign(..., envir=globalenv()). |
R uses lexical scoping – there is a nice introduction to this in the [http://cran.r-project.org/doc/FAQ/R-FAQ.html#Lexical-scoping FAQ on R]. There are no keywords related to scoping, but the [http://stat.ethz.ch/R-manual/R-patched/library/base/html/assign.html assign] function allows you to choose the environment in which a variable is assigned. The global assignment operators , '<<-' and '->>' are effectively shorthand for 'assign(..., envir=globalenv()). |
||
<lang r> |
<lang r>x <- "defined in global env" |
||
x <- "defined in global env" |
|||
foo <- function() |
foo <- function() |
||
{ |
{ |
||
Line 298: | Line 289: | ||
print(x) # "reassigned global value" |
print(x) # "reassigned global value" |
||
} |
} |
||
foo() |
foo()</lang> |
||
</lang> |
|||
=={{header|Tcl}}== |
=={{header|Tcl}}== |
||
Line 387: | Line 377: | ||
The only scope modifier in TI-89 BASIC is the <code>Local</code> command, which makes the variable local to the enclosing program or function rather than global (in some folder). |
The only scope modifier in TI-89 BASIC is the <code>Local</code> command, which makes the variable local to the enclosing program or function rather than global (in some folder). |
||
< |
<lang ti89b>Local x |
||
2 → x |
2 → x |
||
Return x^x</ |
Return x^x</lang> |
||
=={{header|Ursala}}== |
=={{header|Ursala}}== |
||
Line 399: | Line 389: | ||
be switched on and off throughout a source text, and only the symbols |
be switched on and off throughout a source text, and only the symbols |
||
declared when they're on will become visible library entry points. |
declared when they're on will become visible library entry points. |
||
<lang Ursala> |
<lang Ursala>local_shop = 0 |
||
local_shop = 0 |
|||
hidden_variable = 3 |
hidden_variable = 3 |
||
Line 410: | Line 399: | ||
#library- |
#library- |
||
for_local_people = 7 |
for_local_people = 7</lang> |
||
</lang> |
|||
By default, every symbol is visible to every other within the same |
By default, every symbol is visible to every other within the same |
||
file, and multiple declarations of the same symbol are an error, but the |
file, and multiple declarations of the same symbol are an error, but the |
||
Line 417: | Line 405: | ||
scopes within a single file. In this example, the symbol x will have |
scopes within a single file. In this example, the symbol x will have |
||
a value of 1, |
a value of 1, |
||
<lang Ursala> |
<lang Ursala>foo = 1 |
||
foo = 1 |
|||
#hide+ |
#hide+ |
||
Line 427: | Line 414: | ||
#hide- |
#hide- |
||
x = foo |
x = foo</lang> |
||
</lang> |
|||
but it will be 2 in this example, where |
but it will be 2 in this example, where |
||
the #export directive selectively allows an otherwise |
the #export directive selectively allows an otherwise |
||
hidden declaration to be visible outside its enclosing |
hidden declaration to be visible outside its enclosing |
||
scope, and allows name clashes to be resolved by proximity. |
scope, and allows name clashes to be resolved by proximity. |
||
<lang Ursala> |
<lang Ursala>foo = 1 |
||
foo = 1 |
|||
#hide+ |
#hide+ |
||
Line 446: | Line 431: | ||
#hide- |
#hide- |
||
x = foo |
x = foo</lang> |
||
</lang> |
|||
The #hide directives can be arbitrarily nested in matched pairs |
The #hide directives can be arbitrarily nested in matched pairs |
||
to create block structured scope, but doing so is likely to be |
to create block structured scope, but doing so is likely to be |
||
Line 456: | Line 440: | ||
declaration. However, this behavior can be overridden using |
declaration. However, this behavior can be overridden using |
||
the dash operator as shown. |
the dash operator as shown. |
||
<lang Ursala> |
<lang Ursala>#import std |
||
#import std |
|||
cat = 3 |
cat = 3 |
||
a_string = std-cat('foo','bar') |
a_string = std-cat('foo','bar')</lang> |
||
</lang> |
|||
Here, std-cat refers to the concatenation function from the standard |
Here, std-cat refers to the concatenation function from the standard |
||
library, not the locally declared constant by that name. |
library, not the locally declared constant by that name. |