Locale

From J Wiki
< Help(Redirected from Help/Primer/Locale)
Jump to navigation Jump to search


<=   =>

First of all, note that locale is a very different word from local, even though there is only one less letter in the latter.

A locale is a set of global names. There can be several locales, so there can be several sets of globals.

A global name in a locale is distinguished from the same name in other locales by qualifying the name with the addition of the locale name bracketed by _ (underbar) characters. A name qualified by a locale is always a global name.

   abc_def_ =: 2

The above sentence can be read as global abc in locale def is 2.

   abc_base_ =: 4

The above sentence can be read as global abc in locale base is 4.

If the locale name is elided, it is assumed to be base.

   abc__   NB. the same as abc_base_ 
4


If a global name is not qualified with a locale name, then it is in the current locale. The base locale is the current locale unless it has been explicitly changed by executing a verb in a different locale. The following defines abc in the base locale:

   abc =. 6
   abc_base_
6
   abc
6


Since the base locale is the current locale, the names abc and abc_base_ are the same.

The name abc_def_ is clearly different from abc, but so far there is no way of telling that anything special is going on. In what sense are abc and fooin the same (base) locale? And abc and abc_def_ in different locales?

One way of distinguishing is to use the names utility verb that lists global names.

   a =. 23
   b =. 24
   a_q_ =. 25
   w_q_ =. 26
   names 0	NB. 0 lists nouns
a abc b


Your names result may be different, but it will include all global nouns you have defined in the base locale. You should see the a and b that you defined above and note that you do not see the w that was defined in locale q.

To see the names defined in locale q you can do the following:

   names_q_ 0	NB. names in locale q
a w


Nouns a and w are defined in the q locale.

Locales partition global names into different sets, and utilities, such as names, can work with globals in a particular locale.

The real power of locales comes into play with verbs defined in a locale. When a verb executes in a locale it executes with that locale as the current locale, not the base locale.

Let's define a simple verb in the q locale to see how this works.

   f_q_ =. 3 : 'a =: y'


This verb defines global a with its right argument. There can be many different locales, each with their own global a. But when f_q_ executes, it executes in the q locale and the q locale is the current local, and global names it uses are from the q locale. Try the following experiments:

   a =. 23	NB. define a in the base locale
   a_q_ =. 24	NB. define a in locale q
   f_q_ 100	NB. execute f in locale q
100
   a
23
   a_q_
100


Executing f_q_ 100 defined global a_q_ as 100. It did not affect the global a in the base locale.

If a verb explicitly references a name in a locale then that is the global that is affected. For example, define verb g_q_ that defines a in the base locale. You will see that the a in the base locale is defined and the a in the q locale is not changed:

   g_q_ =. 3 : 'a_base_ =: y'	NB. explicit locale name
   g_q_ 200
200
   a
200
   a__
200
   a_q_
100


Locales partition global names into separate sets. In particular, related nouns and verbs, say in a set of utilities, can be defined in their own locale. Their names don't conflict with names in the base or other locales. When you look at your application you can look at just the related globals that are in a particular locale. When a verb runs in a locale it uses globals from that same locale.

The names verb with an argument of 0 lists nouns, with 3 it lists verbs, and with 6 it lists locale names.

   names_q_ 0
a w
   names_q_ 3	NB. verbs
f g
   names 6	NB. locale names
base j q z


The list of locale names is interesting. base and q you know about, but what about j and z ?

The globals in the j and z locales are defined when J starts up and runs the profile.ijs script. The j locale contains things which are useful in building an application and is discussed in the J Online Documentation.

The z locale is very interesting indeed.

<=   =>

Primer Index               Hover to reveal titles   -   Click to access   -   Current page is highlighted
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
45 46 47 48
50 51 52 53 54 55 56 57
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
97 98 99 100 101 102 103 104 105 106