taruss
7/28/2015 11:58:00 PM
On Tuesday, July 28, 2015 at 12:18:49 PM UTC-7, Kaz Kylheku wrote:
> On 2015-07-28, Pascal J. Bourguignon <pjb@informatimago.com> wrote:
> > Jeremy Smith <jeremy@decompiler.org> writes:
> >
> >> Hi,
> >>
> >> I want to make a package, with some functions accessible from outside the
> >> package, and some from within.
> >>
> >> Outside is like 'public' in C++.
> >
> > We say :export in lisp.
> >
> >> Inside is like 'private' in C++.
> >
> > We say :intern in lisp.
>
> What? Interning doesn't have anything to do with privacy.
> Please don't encourage newbies to make false associations.
I agree.
The main lesson is that Common Lisp doesn't have enforced public/private
distinctions, but instead relies on conventions and trusts programmers to
not do the wrong things. This makes the system flexible, but can be a shock
to programmers coming from other more rigid and controlling languages.
Symbols can be internal or external to a package.
By convention, programmers are supposed to only use the external symbols of a
package, since implementers are free to change or remove any of the external
symbols.
Now, in its flexibility, Common Lisp allows you to access the internal symbols,
but the understanding is that one does that at one's own peril.
There are a few tools that can be used to make functions or variable state
inaccessible to the outside, but they rely on somewhat cumbersome scoping, and
at least for functions tend not to be used for that purpose. Shared private
variables created by (top level) closures are more common:
(let ((shared-invisible-variable 0))
(defun get-var () shared-invisible-variable)
(defun inc-var () (incf shared-invisible-variable))
(defun dec-var ()
(when (> shared-invisible-variable 0)
(decf shared-invisible-variable)))
)
For functions all you really have is FLET and LABELS, which could be used, but
tend not to. It also doesn't help that the behavior of FLET or LABELS isn't
defined as a top level form. For that matter, neither is LET, but it tends to
be used more often like that.
So you could do that, but it would be rather cumbersome for any large system,
so nobody really does this in practice. Even the LET form is mainly used more
for illustrative or didactic purposes than for production code.