all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
From: Stefan Monnier <monnier@iro.umontreal.ca>
To: "Zhu Zihao" <all_but_last@163.com>
Cc: emacs-devel@gnu.org
Subject: Re: New lisp library -- cell.el
Date: Fri, 28 Feb 2020 10:43:07 -0500	[thread overview]
Message-ID: <jwvmu92viik.fsf-monnier+emacs@gnu.org> (raw)
In-Reply-To: <b3cbce2.31e2.1708a74bc6f.Coremail.all_but_last@163.com> (Zhu Zihao's message of "Fri, 28 Feb 2020 14:21:11 +0800 (CST)")

Some comments on the code:

> (defsubst cell-option-none ()
>   "Create an option in None variant."
>   cell-option--none-singleton)

Why not use nil for "none"?  That would simplify your code and most
likely would make for much more natural/idiomatic code in the clients of
your library.
[ Traditionally in Lisp when you need something like a "option` (aka
  `Maybe`) type, you use `(cons X nil)` for `some X` and `nil` for `none`.  ]

> (cl-defstruct (cell-box
>                (:constructor nil)
>                (:constructor cell-box--make (-inner))
>                (:copier nil))
>   -inner)

Rather than put the additional "-" in the slot name, you can use
`(:conc-name call-box-)`.

> (defalias 'cell-box-make 'cell-box--make
>   "Create a single mutable unit with INITIAL-VALUE.")
>
> (defalias 'cell-box-inner 'cell-box--inner
>   "Return the inner value of BOX.
> \n(fn BOX)")

Why bother with those indirections?

> (defun cell-weak-get (cell)
>   "Return Some(inner) if reference in CELL still alive, otherwise return None."
>   (let* ((gensym (make-symbol "not-found"))
>          (val (gethash t (cell-weak--inner-table cell) gensym))
>          (found (not (eq gensym val))))
>     (if found
>         (cell-option-some val)
>       (cell-option-none))))

That makes every call to `cell-weak-get` allocate a new symbol only to
throw it away.  I'd recommend you define a top-level constant

    (defconst cell--not-found (make-symbol "not-found"))

instead.  There are other solutions to this problem that might be even
better, tho.  E.g. you could compute `found` as (< 0 (hash-table-count ...).
Better yet, disallow weak cells containing `nil` (since `nil` can't be
GC'd anyway it doesn't make much sense to put it inside a weak-reference)
and you don't need this at all:

    (defun cell-weak-make (inner)
      "Create a cell takes a weak reference of INNER."
      (if (null inner)
          nil ;; We could also signal an error, but returning nil
              ;; allow us to treat nil as its own weak-reference,
        (let* ((cell (cell-weak--internal-make))
              (internal-ht (cell-weak--inner-table cell)))
          (puthash t inner internal-ht)
          cell)))

    (defun cell-weak-get (cell)
      "Return inner if reference in CELL still alive, otherwise return nil."
      (when cell ;; If nil, it's a weak ref to nil.
        ;; The stored content is never nil, so it only returns nil if
        ;; the content was GC'd.
        (gethash t (cell-weak--inner-table cell))))


-- Stefan




  parent reply	other threads:[~2020-02-28 15:43 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-02-28  6:21 New lisp library -- cell.el Zhu Zihao
2020-02-28  8:51 ` Zhu Zihao
2020-02-28 15:43 ` Stefan Monnier [this message]
2020-02-28 16:51   ` New " Zhu Zihao
2020-02-29  3:30     ` Stefan Monnier
2020-02-29  6:48       ` Zhu Zihao
2020-02-29 14:09         ` Stefan Monnier
2020-03-01  7:43           ` Zhu Zihao
2020-03-05 15:52             ` Stefan Monnier

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=jwvmu92viik.fsf-monnier+emacs@gnu.org \
    --to=monnier@iro.umontreal.ca \
    --cc=all_but_last@163.com \
    --cc=emacs-devel@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.