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

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

Re: syntax question for defun


From: David Elson
Subject: Re: syntax question for defun
Date: Thu, 29 Aug 2024 09:45:59 -0700
User-agent: Mozilla Thunderbird

Many *many* thanks tomas !!!

The information you have provided not only answers my basic question (I was able to find the sections by googling effectively disambiguating strings from your fine reply), but gives me some good insight into more properly reading the elisp docs.

Pending a look at elisp lexical analysis code (would be interesting), I'll count on algorithmic "greediness" to disambiguate '->' from '-' (:-), and a new battery in my keyboard to disambiguate it from '>' (:-).

I try to write clear code, but sometimes ... always a good caution, though.

Y'all be well.

-DE

PS Also, per your excellent reply, I will go (cautiously) wild with this (pun intended).

PPS (please pardon the *skippable* after-the-fact context) As for readability, I try to keep the number of columns down, mainly due to the way I tend to split windows.

Some elisp code from 2018, not (yet) intended for anyone (else). I'm sure there are many ways, per knowledgeable folks such as yourself, to improve the approach/style, and I'm all ears. This is just a snippet of what I was starting with:

(require 'object)

...

(defun new-filter-state-machine ()
  (let*
      (
       (fsm (new-object 'filter-state-machine))
       )
    (-> fsm 'add-attr 'state 0)
    (-> fsm 'add-attr 'exp-len -1)
    (-> fsm 'add-method 'reset
        (lambda (this)
          (-> this 'set-state 0)
          (-> this 'set-exp-len -1)
          t
          )
        )
    (-> fsm 'add-method 'process-string
    ...

On 8/28/24 9:57 PM, tomas@tuxteam.de wrote:
On Wed, Aug 28, 2024 at 01:01:33PM -0700, David Elson wrote:
Hello. I have been using emacs for ... a while.

For questions, I have often browsed and browsed (i.e. googled) and ... and
usually find a good explanation. But sometimes I don't find an answer, even
when it is staring right at me.

My question:

To emulate something akin to the PL1/C/C++ arrow notation, I have used:

*(defun -> (object method &rest args) ...)*

and it works.

*Is this valid elisp/lisp?*
It is. From the elisp documentation:
  -- Macro: defun name args [doc] [declare] [interactive] body...
      ‘defun’ is the usual way to define new Lisp functions.  It defines
      the symbol NAME as a function with argument list ARGS ...

... so NAME is a symbol. Again, from the doc:

    A symbol name can contain any characters whatever.  Most symbol names
   are written with letters, digits, and the punctuation characters
   ‘-+=*/’.  Such names require no special punctuation; the characters of
   the name suffice as long as the name does not look like a number.  (If
   it does, write a ‘\’ at the beginning of the name to force
   interpretation as a symbol.)  The characters ‘_~!@$%^&:<>{}?’ are less
   often used but also require no special punctuation.  Any other
   characters may be included in a symbol’s name by escaping them with a
   backslash.

So "->" is not only a legal symbol name, but also one that doesn't need
special escaping to be seen as such. So go wild :)

Actually, Emacs Lisp comes with one function named "-" and another named
">".

That said, this doesn't mean that other people will find your code readable
or enjoyable. It's on you to find that out :-)

If it works only coincidentally, then it might break in the future, when a
pressing emacs issue is resolved in a manner that requires plugging this
"loophole".

If it is formally valid, where does it document the level of flexibility
that allows this syntax?
The Emacs Lisp documentation should be your go-to place for such questions.

Cheers


reply via email to

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