Hi all,

On Mon, Sep 05, 2011 at 08:17:04AM +0200, Alexander Burger wrote:
> A call like
> 
>    (foo> '+Pckg <arg>)
> 
> is in no regard more encapsulating the namespace then
> 
>    (foo.Pckg <arg>)
> 
> but it is more tedious to write and read, takes up two cells more than
> the second (4 versus 2), and eats up performance at each call for the
> method lookup.

If people like to stick with this way of building packages from classes,
how about the following proposal?

It still has the same performance handicap as the straight-forward usage
of classes and methods, but it provides for some syntactic sugar, and
does not impose the cell space penalty.

I would call it "Dynamic Packages", because it does a dynamic runtime
lookup, like in Henrik's style, but with a less noisy syntax.


1. I would put these three expressions into "lib.l"

   # Dynamic packages
   (off "Context")

   (de context Lst
      (setq "Context" Lst) )

   (de dp "Args"
      (push *Class "Args")
      (let "@Msg" (car "Args")
         (def "@Msg"
            (curry ("@Msg") @
               (if (method '"@Msg" '"Context")
                  (pass @)
                  (quit "Not in context" '"@Msg") ) ) ) ) )


2. Then we can define "packages", e.g. '+Add' and '+Mul'. Note that such
   packages are still just normal classes, allowing class variables,
   subclasses, inheritance etc. The difference is only that methods are
   not defined with 'dm' (define method) but with 'dp' (define package
   function):

   (class +Add)

   (dp foo (X Y)
      (+ X Y) )

   (dp bar (X Y Z)
      (+ X Y Z) )


   (class +Mul)

   (dp foo (X Y)
      (* X Y) )

   (dp bar (X Y Z)
      (* X Y Z) )


3. Later, package search order can be specified with 'context'.
   'context' takes any number of classes, to be searched sequentially
   from left to right:

   (context +Add +Mul)

   (println (foo 3 4) (bar 2 3 4))        # Prints 7 9


   (context +Mul +Add)

   (println (foo 3 4) (bar 2 3 4))        # Prints 12 24


One disadvantage of this mechanism (in addition to the runtime overhead)
is that functions defined with 'dp' can only be EXPRs (i.e. evaluating
all arguments).

Any opinions?

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

Reply via email to