Scripted Namespaces

General APL language issues

Scripted Namespaces

Postby paulmansour on Wed Aug 21, 2019 9:53 pm

Consider two ways of creating an anonymous namespace:

Code: Select all
       ns1← {⍵.A←4 ⋄ ⍵.B←5 ⋄ ⍵}⎕NS ''
       ns2← 0 ⎕FIX  ':Namespace' 'A←4' 'B←5' ':EndNamespace'
       ⎕JSON ns1
{"A":4,"B":5}
       ⎕JSON ns2
DOMAIN ERROR: JSON export: item [2] of the right argument cannot be converted (⎕IO=0)
      ⎕JSON ns2


Clearly these are different animals. Furthermore, if we assign a function into ns1, we get the same JSON error as we got with ns2:

Code: Select all
       ⎕JSON ns1
{"A":4,"B":5}
       ns1.foo←{+/⍵}
       ⎕JSON ns1
DOMAIN ERROR: JSON export: item foo of the right argument cannot be converted (⎕IO=0)
      ⎕JSON ns1
      ∧


Is this a bug or a feature? Should []JSON work on a scripted namespace if there are no functions in it?
paulmansour
 
Posts: 311
Joined: Fri Oct 03, 2008 4:14 pm

Re: Scripted Namespaces

Postby paulmansour on Wed Aug 21, 2019 9:59 pm

Also, is there any way to break the connection between a scripted namespace and its script? If would have thought ns3←⎕NS ns2 would have done it, but it does not.
paulmansour
 
Posts: 311
Joined: Fri Oct 03, 2008 4:14 pm

Re: Scripted Namespaces

Postby petermsiegel on Thu Aug 22, 2019 6:28 am

Kludgy (and non-recursive), but perhaps this

    {⍵.⎕NS ⍵.⎕NL ⍳10}ns2
does the job:
Code: Select all
       ns1← {⍵.A←4 ⋄ ⍵.B←5 ⋄ ⍵}⎕NS ''
       ns2← 0 ⎕FIX  ':Namespace' 'A←4' 'B←5' ':EndNamespace'
       ⎕JSON ns1
{"A":4,"B":5}
       ⎕JSON ns2
DOMAIN ERROR: JSON export: item [2] of the right argument cannot be converted (⎕IO=0)
       ⎕JSON ns2
       ∧
       ns3← {⍵.⎕NS ⍵.⎕NL ⍳10}ns2     ⍝ <≡≡≡ HERE
       ⎕JSON ns3
{"A":4,"B":5}

More precise (but still a kludge), convert only vars in any namespace to JSON:
Code: Select all
      jsonSafe← ⎕JSON  {⍵.(⎕NS ⎕NL 2)}
      jsonSafe ns2
{"A":4,"B":5}
petermsiegel
 
Posts: 67
Joined: Thu Nov 11, 2010 11:04 pm

Re: Scripted Namespaces

Postby paulmansour on Thu Aug 22, 2019 10:37 am

Thanks Peter, that's a nice work-a-round, and all I need is top level vars anyway.
paulmansour
 
Posts: 311
Joined: Fri Oct 03, 2008 4:14 pm


Return to Language

Who is online

Users browsing this forum: No registered users and 1 guest