Execute in namespace

APL-related discussions - a stream of APL consciousness.
Not sure where to start a discussion ? Here's the place to be
Forum rules
This forum is for discussing APL-related issues. If you think that the subject is off-topic, then the Chat forum is probably a better place for your thoughts !

Execute in namespace

Postby Budgie on Thu Dec 31, 2009 2:47 pm

I'm in the process of converting an application from bog-standard old-style non-OO APL to the latest Unicode version with all the bells and whistles, and I came across a problem where I want to set the value of a property, where the property name is held in a variable.

So I have this situation
REC is an instance of a class
P is the variable that contains the property name
X is the new value

To get the existing property value is simple: it is REC⍎M
However, I want to set the new value, so my first thought is to code (REC⍎P)←X
Silly me! That doesn't work! I have to code it as ⍎'REC.',P,'←X' which is a bit inelegant.

Is it possible to add an enhancement to APL that would make (REC⍎P)←X work?

(This sort of thing used to crop up often when I was using A+, where they had a solution called "execute in context", which used the % character. The solution to my problem there would be (REC%M)←X. Maybe that's what got me thinking along these lines.)
Jane
User avatar
Budgie
 
Posts: 36
Joined: Thu Nov 26, 2009 9:22 am
Location: Beckenham

Re: Execute in namespace

Postby Phil Last on Thu Dec 31, 2009 6:11 pm

As usual, dfns come to the rescue:
. . . P REC.{⍎⍺,'←⍵'}X
At least it doesn't require the key element names to be quoted.
Alternatively if you want all elements to be arguments to the function:
. . . REC{⍺.{⍎⍺,'←⍵'}/⍵}P X
In the latter case the dfn can be named:
. . . assign←{⍺.{⍎⍺,'←⍵'}/⍵}
. . . REC assign P X
User avatar
Phil Last
 
Posts: 557
Joined: Thu Jun 18, 2009 6:29 pm

Re: Execute in namespace

Postby Phil Last on Thu Dec 31, 2009 7:02 pm

Another alternative syntactically closer to the thing being emulated:
. . . assign←{⍵{⍵ ⍺.{⍎⍺,'←⍵'}⍺⍺}/⍺{⍺}'space name assign value'}
User avatar
Phil Last
 
Posts: 557
Joined: Thu Jun 18, 2009 6:29 pm


Return to APL Chat

Who is online

Users browsing this forum: No registered users and 1 guest