Devon McCormick wrote:
> Can someone explain why this error makes sense?

Here's a simplified example of that error
   3 :'t=:t=.y'0
|domain error

Roger posted a link to
http://www.jsoftware.com/help/dictionary/d310n.htm
(which documents this issue).

A related question is: how are you going to use the global
value if you have a local value?

Usually, you don't want to have the same name mean two different
things in the same context.  And, since this is usually a mistake,
the interpreter treats it as an error.

But what if you want this behavior?

One way to get the global value is to use a different function
(probably a verb) that does not have the local variable:
   3 :'3 :''t''[t=.2[t=:1'0
1

Note also that you can use this technique to assign a value to a 
global variable when a local variable already exists:
   3 :'3 :''t=:y''t=.y'7
7
   t
7

Here's a modified version of your original example which takes
advantage of the above workaround:

rpt=:4 :0 ".
 smoutput x,'= ',(5!:5<'y'),'.'
)

localGlobal=:3 :'var=: ''global in "localGlobal"'''

testLocalGlobal=:3 :0
 var=. 'local in "testLocalGlobal"'
 rpt 'var'
 3 :'var=: localGlobal y'0
 rpt 'var'
 3 :'rpt y' 'var'
)

   testLocalGlobal''
var= 'local in "testLocalGlobal"'.
var= 'local in "testLocalGlobal"'.
var= 'global in "localGlobal"'.

(Of course, you could just take advantage of the fact that verb
localGlobal assigns var, if that made sense in the non-example
context you were working with.)

-- 
Raul

----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm

Reply via email to