Modified assignment with objects: should it work?

Writing and using Classes in Dyalog APL

Modified assignment with objects: should it work?

Postby petermsiegel on Mon Apr 22, 2024 6:59 pm

In Dyalog APL, one can use modified assignment, e.g. +← with ordinary variables, including indexed arrays. The question is: Should this work in a comparable way with class instances (class-based objects)?. Specifically, should it work as expected with methods defined via keywords :Property Default Keyed MyMethod in examples like
Code: Select all
a[1 2 3]+←10 100 1000    ⍝ where ¨a¨ is an object defined via ⎕NEW
where 1 2 3 are keys? Before I post a stripped-down test case (and with the caveat that it might always be my error), I wanted to check if it is supposed to work. Two points:

1. APL allows it; it returns no error; it's happily wrong.
2. It seems to work properly on the first key in the vector 1 2 3, but not apply the modifying function + on subsequent elements.
3. I can't find documentation or examples that might guide me. (And yes, I can't count points properly).

Any experience out there?
petermsiegel
 
Posts: 153
Joined: Thu Nov 11, 2010 11:04 pm

Re: Modified assignment with objects: should it work?

Postby Morten|Dyalog on Tue Apr 23, 2024 7:29 am

Hi Peter!

Yes, it should work, with caveats like there being no guarantees on how many times the get and set functions will be called. However, I see that it does NOT work properly, in the simple example that I constructed, only the first index was updated.

I will log an issue and we'll get back to you when it is fixed.

Having said that, I personally avoid the "f←" construct. It only does what you think when f is a scalar primitive; if f is a defined function there is an implied each and performance goes to the dogs. If I were to start again with a blank sheet, my new APL interpreter would not implement this shortcut. It only had one real use case which was efficient frequency counting using repeated indices, but now we have Key for that.
User avatar
Morten|Dyalog
 
Posts: 458
Joined: Tue Sep 09, 2008 3:52 pm

Re: Modified assignment with objects: should it work?

Postby petermsiegel on Tue Apr 23, 2024 3:59 pm

Morten,
Thanks for testing and updating me/us. I agree it might/should be avoided-- in fact, I had already written a separate method myDict.AddTo to update frequencies of all keys found, but wanted to check the idiomatic myDict+← case for completeness (and to see if there was a performance advantage for either case).

More a curiosity and from my habit of thinking pedagogically about what students will run into.

Best
petermsiegel
 
Posts: 153
Joined: Thu Nov 11, 2010 11:04 pm

Re: Modified assignment with objects: should it work?

Postby AndyS|Dyalog on Tue Apr 30, 2024 3:03 pm

Well we've fixed the limitation in 18.2 and 19.0, and will be available in the next DSS update. We will also be issuing updated 18.2 installation images for all platforms for all users in the near future. New 19.0 images for all platforms for all users will be made available in a slightly longer timeframe.
User avatar
AndyS|Dyalog
 
Posts: 261
Joined: Tue May 12, 2009 6:06 pm

Re: Modified assignment with objects: should it work?

Postby AndyS|Dyalog on Wed May 01, 2024 8:25 am

Actually, I should qualify the statement "we've fixed the limitation" .. for the Keyed Property a,
      a[1 2 3]+←10 100 1000
will work in the forthcoming updates for 18.2 onwards, but for the related
      a[2 2]+←10  ⍝ Duplicate indices
and
      a[1;1]+←3 ⍝ Higher rank
still both fail - and fixes for these last two will take rather to produce than the first !
User avatar
AndyS|Dyalog
 
Posts: 261
Joined: Tue May 12, 2009 6:06 pm

Re: Modified assignment with objects: should it work?

Postby petermsiegel on Wed May 01, 2024 10:12 pm

Thanks all for your diligent work. The semantics of repeated indices (with or without keys) can be surprising, especially for novices. Even for "regular" APL objects (and aficionados), we don't think very often about how serial (one at a time, left-to-right) operations like modified assignment +← must be when indexed arrays are the target, to wit:
Code: Select all
      a←0 0 0                ⍝ vanilla vector
      a[1 2 1 2 1 1]+← 1
      a
0 4 2
Thanks again, all.
petermsiegel
 
Posts: 153
Joined: Thu Nov 11, 2010 11:04 pm


Return to Object Oriented Programming

Who is online

Users browsing this forum: No registered users and 1 guest