Scope modifiers: Difference between revisions
Content added Content deleted
m (→{{header|Java}}: Grammar, numbers must agree) |
(added Ursala) |
||
Line 327: | Line 327: | ||
done</pre> |
done</pre> |
||
As you can see, these are very powerful capabilities which make it trivial to write control structures in next to no Tcl code at all. |
As you can see, these are very powerful capabilities which make it trivial to write control structures in next to no Tcl code at all. |
||
=={{header|Ursala}}== |
|||
There are no variables in Ursala except dummy variables used in |
|||
lambda abstractions, but scope rules govern the visibility of |
|||
constants and function declarations. |
|||
When compiling a library, directives such as #library and #binary can |
|||
be switched on and off throughout a source text, and only the symbols |
|||
declared when they're on will become visible library entry points. |
|||
<lang Ursala> |
|||
local_shop = 0 |
|||
hidden_variable = 3 |
|||
#library+ |
|||
this_public_constant = local_shop |
|||
a_visible_function = + |
|||
#library- |
|||
for_local_people = 7 |
|||
</lang> |
|||
By default, every symbol is visible to every other within the same |
|||
file, but the scope modifiers #hide and #export can create multiple |
|||
scopes within a single file. In this example, the symbol x will have |
|||
a value of 1, |
|||
<lang Ursala> |
|||
foo = 1 |
|||
#hide+ |
|||
foo = 2 |
|||
bar = 3 |
|||
#hide- |
|||
x = foo |
|||
</lang> |
|||
but it will be 2 in this example, where |
|||
the #export directive selectively allows an otherwise |
|||
hidden declaration to be visible outside its enclosing |
|||
scope. |
|||
<lang Ursala> |
|||
foo = 1 |
|||
#hide+ |
|||
#export+ |
|||
foo = 2 |
|||
#export- |
|||
bar = 3 |
|||
#hide- |
|||
x = foo |
|||
</lang> |
|||
The #hide directives can be arbitrarily nested in matched pairs |
|||
to create block structured scope, but doing so is likely to be |
|||
overkill. |
|||
When name clashes occur between imported and locally declared |
|||
symbols, they are resolved by default in favor of the local |
|||
declaration. However, this behavior can be overridden using |
|||
the dash operator as shown. |
|||
<lang Ursala> |
|||
#import std |
|||
cat = 3 |
|||
a_string = std-cat('foo','bar') |
|||
</lang> |
|||
Here, std-cat refers to the concatenation function from the standard |
|||
library, not the locally declared constant by that name. |