Variables local to packages?



On 10/6/11, Panagiotis Papasotiriou <p.j.papasot at gmail.com> wrote:

> example, suppose package foo.mac defines 2 global functions A and B. I am
> looking for a way to declare a variable/function C which would be local to
> the package, so that only functions A and B will have access to it. Such a
> local variable/function should not be visible to any other function defined
> outside foo.mac, so that typing values; in a Maxima session won't even list
> it.

Well, there exists the share package "namespaces" which exposes
CL's package mechanism. I guess it works OK.
You can define A, B, and C in a namespace such that outside that
namespace, A, B, and C refer to other things.

e.g.

---- begin /tmp/foo.mac ----
in_namespace (foo);
A (x) := 1 - x;
B (x) := 2 * x;
C : 12345;
---- end foo.mac ----

---- in your Maxima session ----
load (namespaces);
load_namespace ("/tmp/foo.mac");
A (100);
 => A(100)
foo|A (100);
 => - 99
C;
 => C
C : 5678;
foo|C;
 => 12345
foo|C : 54321;
C;
 => 5678
functions;
 => [foo|A(foo|x), foo|B(foo|x)]
values;
 => [foo|C, C]
---- end Maxima session ----

Some notes:

(1) namespaces are an experimental feature
(2) namespaces are dynamic, not lexical:
  you change the namespace via in_namespace function call;
  the namespace isn't associated with a lexical unit
(3) documentation in comment header of share/contrib/namespaces/namespaces.lisp
(4) in_namespace changes the Lisp package so it has the potential
  to confuse Lisp code which makes assumptions about packages
  (:cl-user, :maxima, etc)
(5) namespaces doesn't hide the existence of symbols

At this point I would say that the biggest unresolved problem is to
make sure that namespaces play nice w/ Lisp, and the biggest improvement
would be to make namespaces lexical instead of dynamic.

> Such a functionality, often called data hiding, is absolutely necessary for
> creating something more complicated than a simple package, so I guess there
> is a way to do it in Maxima, although I was not able to find a way by
> looking to the documentation.

Yeah -- Maxima's user language is pretty weak in that department ...
Occasionally I have daydreams about replacing it with an existing language
or an extension of one. E.g. Python + modified evaluation to allow
partial evaluation of expressions. (Yes, I do understand that's a
pretty big deal.)

best

Robert Dodier