Localising a Namespace within a Class

General APL language issues

Localising a Namespace within a Class

Postby ray on Tue Dec 27, 2022 5:46 pm

For personal reasons*, I dislike Classes and would prefer not to use them. So I am not well versed in their nuances.

But I am using MiServer to make some of my code available via web browsers, and MiServer requires the code being used is supplied as a class. (And MiServer does this job VERY well. Thank you Brian.)

One of my coding preferences is to have NO global variables in the workspace. So I will often create a namespace (called #.G) to hold any "global" variables required (I used to refer to these item as "semi-global"). I will create and localise "G" in the top level function.

So far so good.

However, when running under MiServer, there is no "top level function" for my code to localise "G" in, as it all runs under the class. (The "top" function within the class that MiServer calls is "Compose", but localising the namespace "G" within Compose fails. I think what is required is the equivalent of a "local persistent variable", a "local persistent Namespace".)

So my question is : "Is there a way I can localise #.G within the class" (via a class "constructor" function, or a Field (Public or otherwise) maybe ?).

Currently (because of my Class dislike) the "Class" I use under MiServer contains NO functions, (these are provided via an ":Include" statement) and lots of Fields (and I dont really know how to used these Fields or if I need them at all).

My code is currently running under MiServer without a hitch, until after I have "Stop(ed)" the server. Then I run into problems trying to load a different workspace without first doing a ")CLEAR".
(A ")RESET" I find insufficient, as plays havoc with my code in ⎕SE.) But that is a different problem for another day/year!

Thanking anyone who can help.

Hope you have had, and still are having a great holiday season.

Ray

*I'm stuck in my ways.
Ray Cannon
Please excuse any smelling pisstakes.
User avatar
ray
 
Posts: 221
Joined: Wed Feb 24, 2010 12:24 am
Location: Blackwater, Camberley. UK

Re: Localising a Namespace within a Class

Postby Morten|Dyalog on Tue Dec 27, 2022 9:03 pm

Hi Ray, Merry Christmas!

Is this the kind of thing you are looking for?

Code: Select all
:Class MyClass                 
                               
    :Field Shared G←⎕NS ''     
                               
    ∇ setX arg                 
      :Access Public Instance 
      G.X←arg                 
    ∇                         
                               
    ∇ r←getX                   
      :Access Public Instance 
      r←G.X                   
    ∇                         
                               
:EndClass     
                 
       i1←⎕NEW MyClass
       i2←⎕NEW MyClass
       i1.setX 42 ⍝ Set in one instance
       i2.getX    ⍝ Get from the other
42
User avatar
Morten|Dyalog
 
Posts: 453
Joined: Tue Sep 09, 2008 3:52 pm

Re: Localising a Namespace within a Class

Postby ray on Wed Dec 28, 2022 9:39 pm

Hi Morten, Happy New Year!

Thank you so much, this is exactly what I needed!

Ray
Ray Cannon
Please excuse any smelling pisstakes.
User avatar
ray
 
Posts: 221
Joined: Wed Feb 24, 2010 12:24 am
Location: Blackwater, Camberley. UK


Return to Language

Who is online

Users browsing this forum: No registered users and 1 guest