help-gnu-emacs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Emulating namespaces


From: Pascal J. Bourguignon
Subject: Re: Emulating namespaces
Date: Wed, 08 Dec 2010 15:11:29 -0000
User-agent: Gnus/5.101 (Gnus v5.10.10) Emacs/23.1 (gnu/linux)

LanX <lanx.perl@googlemail.com> writes:

> do I get it right switching the package means that will bind variables
> in the following code to different symbol tables?
>
>> Also, we could just use emacs-cl...
>
> ehm do you wanna tell me that (require 'cl) already provides support
> for namespaces?

No, it doesn't.  emacs-cl is an implementation of Common Lisp written
in emacs lisp, like ejacs is an implementation of JavaScript written
in emacs lisp.


> So why bother?
>
> Could you plz show me some code that reflects the behavior of the perl
> snippet I posted?
>
>
> ---------------
> {
>   package Namespace;
>   our $var=666;
>
>   sub tst {print $var }
>
> }
>
> package Outerspace;
>
> $var=42;
> Namespace::tst(); # prints 666
> ---------------
>
> I'd like to run some tests...

http://www.lisp.se/emacs-cl/

M-x load-library RET load-cl RET
M-x emacs-cl RET
and then in the *Emacs Common Lisp* buffer:


COMMON-LISP-USER> (defpackage :namespace (:use :cl))
#<PACKAGE "NAMESPACE">
COMMON-LISP-USER> (in-package :namespace)
#<PACKAGE "NAMESPACE">
NAMESPACE> (defvar *var* 888)
*VAR*
NAMESPACE> (defun test () (print *var*))
TEST
NAMESPACE> (defpackage :outerspace (:use :cl))
#<PACKAGE "OUTERSPACE">
NAMESPACE> (in-package :outerspace)
#<PACKAGE "OUTERSPACE">
OUTERSPACE> (defvar *var* 42)
*VAR*
OUTERSPACE> (namespace::test)
888 
888
OUTERSPACE> (remove-if-not (lambda (b) (string-match ".*\.lisp" (buffer-name 
b))) (buffer-list))


OUTERSPACE> (list-all-packages)
(#<PACKAGE "OUTERSPACE"> #<PACKAGE "NAMESPACE"> #<PACKAGE "COMMON-LISP-USER"> 
#<PACKAGE "COMMON-LISP"> #<PACKAGE "EMACS-COMMON-LISP-MOP"> #<PACKAGE 
"EMACS-COMMON-LISP"> #<PACKAGE "EMACS-LISP"> #<PACKAGE "KEYWORD">)

OUTERSPACE> (remove-if-not (lambda (n) 
                             (let ((len (length n)))
                               (when (< 3 len) (string= ".el" (subseq n (- len 
3))))))
                            (mapcar (function emacs-lisp:|buffer-name|) 
(emacs-lisp:|buffer-list|)))
("cl-macs.el")
OUTERSPACE> 

-- 
__Pascal Bourguignon__                     http://www.informatimago.com/


reply via email to

[Prev in Thread] Current Thread [Next in Thread]