[Top][All Lists]

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

Re: Proper namespaces in Elisp

From: João Távora
Subject: Re: Proper namespaces in Elisp
Date: Thu, 7 May 2020 22:46:05 +0100

On Thu, May 7, 2020 at 10:10 PM Daniel Colascione <address@hidden> wrote:
On 5/7/20 2:06 PM, Stefan Monnier wrote:

> I thought your previous message was saying you don't like this approach
> (when you said "I don't like reader magic").  Is it that I misunderstood
> or that you don't much like the solution but think that from a pragmatic
> point of view it's still the better option?

I don't like reader magic in general, but I don't think of the CL
approach as being all that magical: it has uniform rules and a long
history. CL namespaces *are* reader magic all right, but it's reader
magic with which a lot of people are familiar

Not only that, but CL the reader itself is programmable. So it's really
_not_ "magic", it's all (hyper)spec'ed!


However, Daniel, my proposal doesn't have much magic.  Is is really
dumb ;-).  Here's the gist of it (a pure elisp solution using advice,
very lightly tested)

(require 'cl-lib)

  (advice-add 'read :around #'read-aware-of-shorthands)
  (defvar shorthand-shorthands '(("^vlp-" . "very-long-prefix-"))))

(defun shorthand-expand (form)
  (cond ((consp form)
         (setcar form (shorthand-expand (car form)))
         (setcdr form (shorthand-expand (cdr form))))
        ((arrayp form)
         (cl-loop for i from 0 for e across form
                  do (aset form i (shorthand-expand e))))
        ((symbolp form)
         (let ((name (symbol-name form)))
           (cl-loop for (short-pat . long-pat) in shorthand-shorthands
                    do (setq name
                             (replace-regexp-in-string short-pat long-pat
                    finally (setq form (intern name))))))

(defun read-aware-of-shorthands (oldread &rest stuff)
  (let ((form (let ((obarray (obarray-make)))
                (apply oldread stuff))))
    (shorthand-expand form)))

(defun vlp-foo () 42) ;; => very-long-prefix-foo
(vlp-foo) ;; => 42

(defun vlp-aref1 (array) (aref array 1))
(vlp-aref1 [2 vlp-shiiz 4]) ;; => very-long-prefix-shiiz

(let ((shorthand-shorthands nil))
  ;; pretend this was some other file
  (eval (car (read-from-string "(vlp-foo)"))) ;; => errors
  (eval (car (read-from-string "(very-long-prefix-foo)"))) ;; => 42

reply via email to

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