Copy a class into a namespace

Writing and using Classes in Dyalog APL

Copy a class into a namespace

Postby giangiquario on Tue Apr 05, 2011 12:36 pm

Dyalog 12.1 Classic

I am not able to copy one (or more) class into a Namespace.

Let myClass be a Class
I tried :

NS←⎕NS 'myClass'
DOMAIN ERROR


The language reference:
Create Namespace: {R}←{X}⎕NS Y
In the first case, Y must be a simple character scalar, vector, matrix or a nested vector
of character vectors identifying zero or more workspace objects to be copied into the
namespace X. The identifiers in X and Y may be simple names or compound names
separated by '.' and including the names of the special namespaces '#', '##' and
'⎕SE'.
The namespace X is created if it doesn't already exist. If the name is already in use for
an object other than a namespace, APL issues a DOMAIN ERROR.
If X is omitted, an unnamed namespace is created.
The objects identified in the list Y are copied into the namespace X.
User avatar
giangiquario
 
Posts: 46
Joined: Thu Nov 26, 2009 8:55 am
Location: Milano, Italia

Re: Copy a class into a namespace

Postby uwejanza on Wed Apr 06, 2011 3:15 pm

To create the copy of a class in some namespace you have to fix the source of the class inside the target namespace.
Here is a function that does the trick, and some little extra checking. Left argument is the name of the target namespace, right argument is the name of the class to be copied. Both names must be fully (well, at least sufficiently) qualified.

Code: Select all
⍙ copyclass ∆;∆∆
⍝p copy class ∆ to namespace ⍙
 :If 9.4≠⎕NC⊂⍕∆
   11 ⎕SIGNAL⍨'NOT NAME OF A CLASS: 9.4≠⎕NC⊂(',⍕∆,')'
 :ElseIf 9.1≠⎕NC⊂⍕⍙
   11 ⎕SIGNAL⍨'NOT A NAMESPACE: 9.1≠⎕NC⊂(',⍕⍙,')'
 :ElseIf 0≠⎕NC(⍕⍙),∆∆←{'.',⍵↑⍨1-'.'⍳⍨⌽⍵}⍕∆
   11 ⎕SIGNAL⍨'WILL NOT OVERWRITE EXISTING ',(⍕⍙),∆∆
 :Else
   ∆←⎕SRC⍎⍕∆
   :With ⍕⍙
     ⎕FIX ∆
   :EndWith
 :EndIf
uwejanza
 
Posts: 19
Joined: Tue Mar 09, 2010 2:01 pm
Location: Nürnberg, Germany

Re: Copy a class into a namespace

Postby giangiquario on Tue Apr 12, 2011 1:09 pm

I thank you for your help.

My goal is to a copy of a set of classes into MyNamespace(I do not want references to original classes; I need actual clones).

Unfortunatly by using []SRC and []FIX , sometimes it happens that it is not possible to fix the class into MyNamespace.
E.g. I get a DOMAIN ERROR when I try to []FIX the following (the BaseClass is not yet defined):

Code: Select all
':Class MyClass: BaseClass' ':EndClass'


A Class is an APL object. I think that every APL object should be copied without any hierarchic dependency.
User avatar
giangiquario
 
Posts: 46
Joined: Thu Nov 26, 2009 8:55 am
Location: Milano, Italia

Re: Copy a class into a namespace

Postby uwejanza on Tue Apr 12, 2011 4:37 pm

A Class is an APL object. I think that every APL object should be copied without any hierarchic dependency.


Taking that sentence literally leads to a different solution: let us use ⎕CY to copy an APL "object" (according to APL old-speak).

To use ⎕CY the workspace containing the original classes to be copied must be saved on disk, e.g. as TheSavedWorkspace.dws. Then we can

'TheNamespaceWhereMyOriginalClassesAreStored.MyClass' MyNamespace.⎕CY 'TheSavedWorkspace.dws'

This seems to work even when the base class of the class to be copied does not exist (yet) in MyNamespace. But even after copying MyClass to MyNamespace you will not be able to fix MyNamespace.MyClass in the editor as long as is relates itself to BaseClass and MyNamespace.BaseClass is missing. This is the tradeoff we have to accept for the sake of getting class inheritance.
uwejanza
 
Posts: 19
Joined: Tue Mar 09, 2010 2:01 pm
Location: Nürnberg, Germany

Re: Copy a class into a namespace

Postby Vince|Dyalog on Wed Apr 13, 2011 1:37 pm

There is an RFE (Request for Enhancement) logged for this issue, 004109.
Vince|Dyalog
 
Posts: 412
Joined: Wed Oct 01, 2008 9:39 am


Return to Object Oriented Programming

Who is online

Users browsing this forum: No registered users and 1 guest