Hi Abel,

> Also mmamkin connected with me directly and also suggested to use prefix "pg-"
> for whole library not only for public functions. I think that I probably
> should to use namespaces to hide private functions.

I would also recommend namespaces.


> It would be nice if someone would share with me best practice for using them
> bcs I still do not have deep understanding in their usage.

There are some examples for namespaces in the PicoLisp distribution, in the
libraries @lib/gis.l, @lib/android.l and @lib/vip.l.

PilBox (https://software-lab.de/PilBox.tgz) also uses namespaces, it even
depends on them, because it runs several apps alternatingly in the same PicoLisp
process, including different database models. To see typical PilBox apps, look
at the internet radio (https://software-lab.de/radio.zip), the bignum calculator
(https://software-lab.de/calc.zip) or the general dummy demo
(https://software-lab.de/demo.zip).


In general, I use the following strategy:

1. *libraries* set their namespace at the very beginning

      (symbols 'newlib 'pico)

   or, if needed,

      (symbols 'mylib 'otherlib 'pico)

   This puts the new library in front of the search order, causing new symbols
   being created in that namespace.


2. *before* a symbol appears for the first time in the library source, a call to
   'local' is recommended

      (local) foo

   or

      (local) [foo bar]

   Note that it is irrelevant where the symbol's value may be *defined* in the
   source. Important is where it is *read*. Namespaces care only about the
   visibility of symbols.

   Also note that the semantics of 'local' are a bit unusual. It takes no
   arguments, but *reads* the next expression in the current input stream so
   that all symbols contained in it are created newly (= interned) in this
   namespace.


3. In an *application* of that lib, I put it usually at the end, using the
   "set search order" syntax

      (symbols '(pico mylib))

   or, if the app defines its own private namespace, I define it in the main
   (initially loaded) source

      (symbols 'demo 'gis 'pico 'android)

   and use the "set search order" syntax in all other files of the application
   if needed as

      (symbols '(demo gis pico android)
      (symbols '(radio pico android))

   Such calls with a complete search order list may occur at various places,
   allowing you to fine-tune how symbols are read.

   Besides this, you can always access symbols from other namespaces (if they
   are not in the current search order (but that namespace is), or are shadowed
   by some symbol with the same name in a namespace earlier in the order) as

      mylib~foo


I hope this helps for a first start. This whole namespace issue is not trivial,
but a powerful tool to organize one's projects.

☺/ A!ex

-- 
UNSUBSCRIBE: mailto:picolisp@software-lab.de?subject=Unsubscribe

Reply via email to