Scope/Function names and labels: Difference between revisions

m
J draft
(→‎{{header|C}}: Fixed the worst of the C errors. This is not the competition page for the ugliest code.)
m (J draft)
Line 297:
 
There are no labels in either language.
 
=={{header|J}}==
 
'''NAMES'''
 
J is scoped lexically but with non-nested block scope. Nesting can be emulated, where that is required. But deep nesting can be difficult to understand and debug so the coder is penalized (with a bit of extra work) for implementing deep nesting.
 
Specifically, J provides two scopes for user defined names:
 
'''Local''' scope, names defined using =. will have local scope if a block scope exists.
 
<lang j> a=. 1</lang>
 
'''Locale''' scope, names defined using =: will have locale scope (and the '''base''' locale is used by default).
 
<lang j> b=: 2</lang>
 
Names may include a locale qualifier. Locale qualifiers contain two '''_''' characters, are a suffix on what would be the unqualified name. Locale qualifiers come in two forms: absolute and relative. Relative locale qualifiers use another name in the current locale to identify the target locale. Absolute locatives place the locale name between the two '''_''' characters, while relative locatives name the locale reference following the pair of '''_''' characters.
 
<lang j> c_thingy_=: 3
d=: <'test'
e__d=: 4
b + e_test_
6</lang>
 
If a local definition exists for a name, it is an error to use use =: to assign that name (use a locative instead of the unqualified name if you really need to do this).
 
<lang j>verb define ''
f=. 6
g=: 7
g=. 8
g=: 9
)
|domain error
| g =:9</lang>
 
Meanwhile, there is a global current locale (default: '''base''') but each verb's definition is evaluated with the current locale being the locale where that verb was defined.
 
Name resolution first checks for local definitions for a name, and if none are found the current locale is checked, and if nothing is found there the current locale's search path is used to check other locales. By default the '''z''' locale is included as the last element of this path. (And, since the z locale contains the system defined words, removing it from a locale's path will break most things in that locale.)
 
The current locale during the execution of a verb is the locale where that verb was defined. But the current locale can be changed when the current ''named'' verb finishes using <code>18!:4</code>.
 
If you want to emulate static nested scope (or dynamic scope) you need to arrange for the desired behavior using the locale path mechanism.
 
'''LABELS'''
 
Labels are available in explicit definitions, and are names beginning with '''label_''' and ending with a '''.'''. You may use them with a goto which is a name beginning with '''goto_''' and ending with a '''.'''. Use of labels is restricted to the scope where they are defined, and they cannot be used to enter control structures (except in the sense of the normal entry point).
 
<label j>example=:3 :0
if. y do.
echo 0
goto_a.
echo 1
else.
echo 2
goto_a.
echo 3
end.
echo 4
label_a.
echo 5
)
example 1
0
5
example 0
2
5</lang>
 
=={{header|jq}}==
6,962

edits