all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
From: Emanuel Berg <incal@dataswamp.org>
To: help-gnu-emacs@gnu.org
Subject: Re: Printing alist pairs to a dedicated buffur
Date: Fri, 26 Apr 2024 02:27:17 +0200	[thread overview]
Message-ID: <87jzkl9dei.fsf@dataswamp.org> (raw)
In-Reply-To: SnL2cLHThbJa57KzSzv9ORQxcZOa5__743NoLA9oZ-QSHeOpxeVscdv9HSmX6-aujDgEGAvd1qp7BzLPR7emBbB3dYML9CB8VMPzElmkQkk=@protonmail.com

Heime wrote:

>> Don't use `defvar' if it can be avoided as that creates
>> global dynamic/special variables,` setq' creates global
>> static/lexical variables which isn't much better - unless
>> there is a variable by that name present, then that is used
>> instead. That kind of `setq' use is not wrong, actually it
>> is good.
>
> So it is actually good, but then still make a commotion
> about it. This way of commenting in not productive at all.

`setq' to set variables is good, to create global variables
with it is often not necessary and for the compiler not to
warn you also have to use `defvar', that is totally confusing.

(defvar global-lexical)
(setq global-lexical 10)
(special-variable-p 'global-lexical) ; nil

Also setq is error-prone as make a typo, the intended variable
with not be set but instead a global one will be created.
But the byte compiler will warn you about that if it happens.

Another solution would be one function to set available
variables, and one to create global ones. The "set available"
one would signal if such a variable isn't present, the global
one if there were already such a variable. But again, this is
what makes Lisp interesting.

> My defvar is used to store values that I need to access when
> I am working on a buffer. I can add to it as I modify the
> buffer, or delete from it. I cannot see why all this fuss
> not to use global variables.

Certainly they are there if you want to use them (pun).
Lisp is a maximalist language, anything and everything
included. Some people think global variables are okay, other
people think they should be avoided if there isn't any good
reason to have them.

Why is a static/lexical global variable better than
a dynamic/special? Good question, we must write some Elisp to
test what happens if that name collides with other source.

I wrote some Elisp that would show another option approach, so
one would always have `let' for static/lexical variables to
facilitate coding, and another construct for options to set
the behavior of whatever was enclosed. Now I don't really
understand it anymore 100%, but I yank it here [last] for your
viewing pleasure :)

But it will be the way it is now, this is just endless
theorizing because Lisp is very powerful, but also very
complicated compared to other languages.

;;; -*- lexical-binding: t -*-
;;
;; this file:
;;   https://dataswamp.org/~incal/emacs-init/vars-opts.el

(require 'cl-lib)

(defun check-binders (binds)
  (cl-loop
    for (n _) in binds
    with no-opt
    do (unless (special-variable-p n)
         (push (symbol-name n) no-opt) )
    finally (when no-opt
              (error "No option%s: %s"
                     (if (< 1 (length no-opt))
                         "s"
                       "")
                     (mapconcat #'identity (reverse no-opt) ", ") ))))

(defmacro opts (binds &rest body)
  (declare
    (indent 1)
    (debug let) )
  (check-binders binds)
  `(let ,binds ,@body) )

;; (opts ((fill-column 1)
;;        (a 0) )
;;   u) ; No option: a
;;
;; (defvar dynavar 2000)
;;
;; (opts ((dynavar 3000)
;;        (fill-column 1)
;;        (a 0)
;;        (y 2) )
;;   y) ; No options: a, y
;;
;; (opts ((dynavar 3000)
;;        (fill-column 10) )
;;   (delete-char 2)
;;   (fill-paragraph) ) ; eval me

(provide 'vars-opts)

-- 
underground experts united
https://dataswamp.org/~incal




  reply	other threads:[~2024-04-26  0:27 UTC|newest]

Thread overview: 23+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-04-19 12:57 Printing alist pairs to a dedicated buffur Heime
2024-04-19 13:24 ` Manuel Giraud via Users list for the GNU Emacs text editor
2024-04-19 19:26   ` Heime
2024-04-19 19:42     ` Heime
2024-04-20  9:55       ` Manuel Giraud via Users list for the GNU Emacs text editor
2024-04-20 10:08         ` Emanuel Berg
2024-04-25 11:13           ` Heime
2024-04-26  0:27             ` Emanuel Berg [this message]
2024-04-25 15:55           ` [External] : " Drew Adams
2024-04-25 16:18             ` Heime
2024-04-25 22:43               ` Emanuel Berg
2024-04-25 22:29             ` Emanuel Berg
2024-04-25 23:38               ` Drew Adams
2024-04-26  0:03                 ` Emanuel Berg
2024-04-26 15:55                   ` Drew Adams
2024-04-26 16:57                     ` Heime
2024-04-26 17:27                       ` Yuri Khan
2024-04-27 22:20                         ` Heime
2024-04-26  0:41                 ` Emanuel Berg
2024-04-26  1:08                 ` Emanuel Berg
2024-04-20 16:37         ` Heime
2024-04-20 17:23           ` Heime
2024-04-20 20:48             ` Yuri Khan

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=87jzkl9dei.fsf@dataswamp.org \
    --to=incal@dataswamp.org \
    --cc=help-gnu-emacs@gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.