all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
* Proper use of function form
@ 2020-04-26 23:27 Tim Johnson
  2020-04-27  0:05 ` Michael Heerdegen
                   ` (2 more replies)
  0 siblings, 3 replies; 7+ messages in thread
From: Tim Johnson @ 2020-04-26 23:27 UTC (permalink / raw)
  To: MLEmacs

Here is a key definition:

(define-key jinja2-mode-map (kbd "C-c i") #'jinja2-insert-var)

As I understand it, the sharp-quote (#') tells the compiler that 
jinja2-insert-var is meant to be a function

The following quotation:

"it is always good practice to sharp quote every symbol that is the name 
of a function, whether it's going into a |mapcar|, an |apply|, a 
|funcall|, or anything else."

Can be found at 
https://endlessparentheses.com/get-in-the-habit-of-using-sharp-quote.html

fuurther text there describes the special 'function form for which the 
sharp-quote is a shorthand.

Suppose we are processing a sexp of alternating strings and symbols:

(setq my-keypairs '("h" my-h-func "g" my-g-func "f" my-f-func))

and using iteration to *programmatically* call define-key for multiple 
key definitions as in

(define-key mymap                               ;; add to keymap
     (kbd (concat ldr (nth ndx mylist)))    ;; sequence as in "g"
     (nth (+ ndx 1) mylist))                      ;; command as in my-g-func

The form above is indeed a snippet from a defun that I have used for years

Would it be better to change (nth (+ ndx 1) mylist) to (function (nth (+ 
ndx 1) mylist)) ?

I note that help for 'function states a preference for using it with 
function objects, but says nothing about processing a list. Hence I am 
lobbying wiser heads than mine for opinions before I implement something 
that might cause subtle problems down the road.

Emacs is GNU Emacs 26.3 (build 2, x86_64-pc-linux-gnu, X toolkit, Xaw 
scroll bars)
on ubuntu 16.04

Thanks

-- f
Tim
tj49.com



^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: Proper use of function form
  2020-04-26 23:27 Proper use of function form Tim Johnson
@ 2020-04-27  0:05 ` Michael Heerdegen
  2020-04-27  0:43   ` Tim Johnson
  2020-04-27  0:48 ` Noam Postavsky
  2020-04-27  8:06 ` Emanuel Berg via Users list for the GNU Emacs text editor
  2 siblings, 1 reply; 7+ messages in thread
From: Michael Heerdegen @ 2020-04-27  0:05 UTC (permalink / raw)
  To: help-gnu-emacs

Tim Johnson <tim@akwebsoft.com> writes:

> Would it be better to change (nth (+ ndx 1) mylist) to
> (function (nth > (+ ndx 1) mylist)) ?

No:

"Like `quote', but preferred for objects which are functions.
In byte compilation, `function' causes its argument to be handled by
the byte compiler.  `quote' cannot do that."

"(nth > (+ ndx 1) mylist)" is not a function - it is an
expression that will evaluate to a function.  Since `function' acts like
`quote', using this special form would prevent that evaluation, so it
won't work.

The underlying problem is that your expression is evaluated at
run-time.  Useful for the compiler would only be a function name known
at compile time.  If you know the function name at compile time, you
don't want to eval it at run time, so you want to `quote' anyway.

So what can you do?  Since we don't have a multiple-define-key function,
you can just stay with your loop as is - there is nothing wrong with
that, but you'll not get compiler warnings like "unknown function".  You
can also use a macro that would expand to a sequence of `define-key'
calls at compile time.  Or construct the whole keymap at compile time,
using list functions (or `backquote').  But that's rather uncommon.
Most people just write the key definition calls out or just don't care -
even in the Emacs sources.


Michael.




^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: Proper use of function form
  2020-04-27  0:05 ` Michael Heerdegen
@ 2020-04-27  0:43   ` Tim Johnson
  0 siblings, 0 replies; 7+ messages in thread
From: Tim Johnson @ 2020-04-27  0:43 UTC (permalink / raw)
  To: help-gnu-emacs


On 4/26/20 4:05 PM, Michael Heerdegen wrote:
> Tim Johnson <tim@akwebsoft.com> writes:
>
>> Would it be better to change (nth (+ ndx 1) mylist) to
>> (function (nth > (+ ndx 1) mylist)) ?
> No:
>
> "Like `quote', but preferred for objects which are functions.
> In byte compilation, `function' causes its argument to be handled by
> the byte compiler.  `quote' cannot do that."
>
> "(nth > (+ ndx 1) mylist)" is not a function - it is an
> expression that will evaluate to a function.  Since `function' acts like
> `quote', using this special form would prevent that evaluation, so it
> won't work.
>
> The underlying problem is that your expression is evaluated at
> run-time.  Useful for the compiler would only be a function name known
> at compile time.  If you know the function name at compile time, you
> don't want to eval it at run time, so you want to `quote' anyway.
>
> So what can you do?  Since we don't have a multiple-define-key function,
> you can just stay with your loop as is - there is nothing wrong with
> that, but you'll not get compiler warnings like "unknown function".  You
> can also use a macro that would expand to a sequence of `define-key'
> calls at compile time.  Or construct the whole keymap at compile time,
> using list functions (or `backquote').  But that's rather uncommon.
> Most people just write the key definition calls out or just don't care -
> even in the Emacs sources.

Thanks Michael. I will leave will enough alone.

cheers

-- 
Tim
tj49.com




^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: Proper use of function form
  2020-04-26 23:27 Proper use of function form Tim Johnson
  2020-04-27  0:05 ` Michael Heerdegen
@ 2020-04-27  0:48 ` Noam Postavsky
  2020-04-27  1:58   ` Tim Johnson
  2020-04-27  8:06 ` Emanuel Berg via Users list for the GNU Emacs text editor
  2 siblings, 1 reply; 7+ messages in thread
From: Noam Postavsky @ 2020-04-27  0:48 UTC (permalink / raw)
  To: Tim Johnson; +Cc: MLEmacs

On Sun, 26 Apr 2020 at 19:27, Tim Johnson <tim@akwebsoft.com> wrote:

> (setq my-keypairs '("h" my-h-func "g" my-g-func "f" my-f-func))
>
> and using iteration to *programmatically* call define-key for multiple
> key definitions as in
>
> (define-key mymap                               ;; add to keymap
>      (kbd (concat ldr (nth ndx mylist)))    ;; sequence as in "g"
>      (nth (+ ndx 1) mylist))                      ;; command as in my-g-func
>
> The form above is indeed a snippet from a defun that I have used for years
>
> Would it be better to change (nth (+ ndx 1) mylist) to (function (nth (+
> ndx 1) mylist)) ?

No, you would put the function in the `my-keypairs' initializer:

(setq my-keypairs `("h" ,#'my-h-func "g" ,#'my-g-func "f" ,#'my-func))

I usually don't bother sharp-quoting in lists of functions though.



^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: Proper use of function form
  2020-04-27  0:48 ` Noam Postavsky
@ 2020-04-27  1:58   ` Tim Johnson
  0 siblings, 0 replies; 7+ messages in thread
From: Tim Johnson @ 2020-04-27  1:58 UTC (permalink / raw)
  To: help-gnu-emacs


On 4/26/20 4:48 PM, Noam Postavsky wrote:
> On Sun, 26 Apr 2020 at 19:27, Tim Johnson <tim@akwebsoft.com> wrote:
>
>> (setq my-keypairs '("h" my-h-func "g" my-g-func "f" my-f-func))
>>
>> and using iteration to *programmatically* call define-key for multiple
>> key definitions as in
>>
>> (define-key mymap                               ;; add to keymap
>>       (kbd (concat ldr (nth ndx mylist)))    ;; sequence as in "g"
>>       (nth (+ ndx 1) mylist))                      ;; command as in my-g-func
>>
>> The form above is indeed a snippet from a defun that I have used for years
>>
>> Would it be better to change (nth (+ ndx 1) mylist) to (function (nth (+
>> ndx 1) mylist)) ?
> No, you would put the function in the `my-keypairs' initializer:
>
> (setq my-keypairs `("h" ,#'my-h-func "g" ,#'my-g-func "f" ,#'my-func))
>
> I usually don't bother sharp-quoting in lists of functions though.
>
Wow! I had been thinking of that myself, but not fully understanding how 
symbols are evaluated in descending layers in sexps, this is a great 
second opinion.

Thank you Noam.

... and keep 'em coming

-- 
Tim
tj49.com




^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: Proper use of function form
  2020-04-26 23:27 Proper use of function form Tim Johnson
  2020-04-27  0:05 ` Michael Heerdegen
  2020-04-27  0:48 ` Noam Postavsky
@ 2020-04-27  8:06 ` Emanuel Berg via Users list for the GNU Emacs text editor
  2020-04-27 19:12   ` Tim Johnson
  2 siblings, 1 reply; 7+ messages in thread
From: Emanuel Berg via Users list for the GNU Emacs text editor @ 2020-04-27  8:06 UTC (permalink / raw)
  To: help-gnu-emacs

Tim Johnson wrote:

> Here is a key definition:
>
> (define-key jinja2-mode-map (kbd "C-c i") #'jinja2-insert-var)
>
> As I understand it, the sharp-quote (#') tells the
> compiler that jinja2-insert-var is meant to be
> a function

Yes, that's what happens. I don't know what the
byte-compiler does with that information tho, maybe it
is just a matter of it being able to warn you if that
function doesn't exist, since with the hash-quote, it
knows where to look specifically...

But there is also the advantage of you being able to see
that something is, or supposed to be, a function.

Perhaps font-lock could also be told to give it
a special color, e.g. `font-lock-function-name-face', if
desired - to increase seeing/reduce reading
even further.

> The following quotation:
>
> "it is always good practice to sharp quote every
> symbol that is the name of a function, whether it's
> going into a mapcar, an apply, a funcall, or
> anything else."

Well, that's what I do and it always worked. But if you
seek a more scientific documentation and practical
HOWTO, surely this is documented in the official
manual(s)?

> (setq my-keypairs '("h" my-h-func "g" my-g-func "f" my-f-func))
>
> and using iteration to *programmatically* call
> define-key for multiple key definitions as in
>
> (define-key mymap                          ;; add to keymap
>     (kbd (concat ldr (nth ndx mylist)))    ;; sequence as in "g"
>     (nth (+ ndx 1) mylist))                ;;
> command as in my-g-func
>
> The form above is indeed a snippet from a defun that
> I have used for years
>
> Would it be better to change (nth (+ ndx 1) mylist) to
> (function (nth (+ ndx 1) mylist)) ?

But even if that had worked all the advantages mentioned
so far would be lost, right?

Maybe you could initially instead of the quote use the
backquote/comma, or `list', with hash-quote?

-- 
underground experts united
http://user.it.uu.se/~embe8573
https://dataswamp.org/~incal




^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: Proper use of function form
  2020-04-27  8:06 ` Emanuel Berg via Users list for the GNU Emacs text editor
@ 2020-04-27 19:12   ` Tim Johnson
  0 siblings, 0 replies; 7+ messages in thread
From: Tim Johnson @ 2020-04-27 19:12 UTC (permalink / raw)
  To: help-gnu-emacs


On 4/27/20 12:06 AM, Emanuel Berg via Users list for the GNU Emacs text 
editor wrote:
> Tim Johnson wrote:
>
>> Here is a key definition:
>>
>> (define-key jinja2-mode-map (kbd "C-c i") #'jinja2-insert-var)
>>
>> As I understand it, the sharp-quote (#') tells the
>> compiler that jinja2-insert-var is meant to be
>> a function
> Yes, that's what happens. I don't know what the
> byte-compiler does with that information tho, maybe it
> is just a matter of it being able to warn you if that
> function doesn't exist, since with the hash-quote, it
> knows where to look specifically...
>
> But there is also the advantage of you being able to see
> that something is, or supposed to be, a function.
>
> Perhaps font-lock could also be told to give it
> a special color, e.g. `font-lock-function-name-face', if
> desired - to increase seeing/reduce reading
> even further.
>
>> The following quotation:
>>
>> "it is always good practice to sharp quote every
>> symbol that is the name of a function, whether it's
>> going into a mapcar, an apply, a funcall, or
>> anything else."
> Well, that's what I do and it always worked. But if you
> seek a more scientific documentation and practical
> HOWTO, surely this is documented in the official
> manual(s)?
>
>> (setq my-keypairs '("h" my-h-func "g" my-g-func "f" my-f-func))
>>
>> and using iteration to *programmatically* call
>> define-key for multiple key definitions as in
>>
>> (define-key mymap                          ;; add to keymap
>>      (kbd (concat ldr (nth ndx mylist)))    ;; sequence as in "g"
>>      (nth (+ ndx 1) mylist))                ;;
>> command as in my-g-func
>>
>> The form above is indeed a snippet from a defun that
>> I have used for years
>>
>> Would it be better to change (nth (+ ndx 1) mylist) to
>> (function (nth (+ ndx 1) mylist)) ?
> But even if that had worked all the advantages mentioned
> so far would be lost, right?
>
> Maybe you could initially instead of the quote use the
> backquote/comma, or `list', with hash-quote?
>
Thanks Emanuel. Of course you know that it was your use of the 
sharp-quote in a response to an earlier question by me that prompted me 
to ask this latter question. I will play around with with subsequent 
advice when I have the time.

cheers

-- 
Tim
tj49.com




^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2020-04-27 19:12 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2020-04-26 23:27 Proper use of function form Tim Johnson
2020-04-27  0:05 ` Michael Heerdegen
2020-04-27  0:43   ` Tim Johnson
2020-04-27  0:48 ` Noam Postavsky
2020-04-27  1:58   ` Tim Johnson
2020-04-27  8:06 ` Emanuel Berg via Users list for the GNU Emacs text editor
2020-04-27 19:12   ` Tim Johnson

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.