This issue is not specific to tacit definitions -- you will encounter
this in all uses of all variables.
For example, try this in a script:
a=. 1
3 :0 ''
echo a
)
Unless you also have a global definition for a, when you run that
script, you would get an error:
|noun result was required: script
| echo a
(And the same thing would happen if you used the variable b instead of
the variable a ...)
What's happening here is that scripts are loaded by a verb. (So that
a=.1 is local to that verb.)
In a j session, unless you are suspended in an explicit verb (using
the debugging features), a statement like this:
a=. 1
actually behaves like this:
a=: 1
Perhaps this should instead throw an error? But, anyways, that's how
it currently works. This can be convenient when testing code fragments
using copy and paste, but it does require some understanding.
But, inside a local definition, a=.1 assigns the value 1 to a local
definition of a, and that local definition is only accessible in that
verb.
I could go on and talk about the reasons for this and how to get
differing features if that is needed, but hopefully, for now, this is
sufficient for you?
Thanks,
--
Raul
On Thu, Jul 15, 2021 at 5:01 AM 'Rudolf Sykora' via Programming
<[email protected]> wrote:
>
> Dear list,
>
> having a script file A.ijs with the contents
>
> a =. 1
> f1 =. a&+
> f2 =. 3 : 0
> a&+y
> )
> f1 0
> f2 0
>
> and loading it with
>
> a =. 2
> loadd'A.ijs'
>
> I see that the value 1 of 'a' is used for f1, but a value of a *global*
> variable 'a' is used for f2. This bothers me a lot. Hence just by
> changing from a tacit to explicit form can lead to a surprise.
>
> I eventually found a mention of a similar effect in 'Learning J', but I
> do not understand why it has been designed this way... So what is the
> way to have local variables (ie, with the scope of a script file) that are
> visible (as if globals) to the functions within the script? Does one
> have to create a locale to this end?
>
> Thanks for any comments and explanations.
>
>
> Best regards
> Ruda
> ----------------------------------------------------------------------
> For information about J forums see http://www.jsoftware.com/forums.htm
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm