Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

difference between (defalias 'A (symbol-function 'B)) and (defalias 'A 'B)

Tags:

I was reading subr.el and saw this code:

(defalias 'backward-delete-char 'delete-backward-char) (defalias 'search-forward-regexp (symbol-function 're-search-forward)) 

Interestingly, the first line doesn't use symbol-function while the second line does. The only difference I know of these two ways of using defalias is that the help for backward-delete-char (the first one) displays that it is an alias for delete-backward-char while the help for search-forward-regexp doesn't.

Is there a case when the second way is better than the first way?

like image 248
Yoo Avatar asked Oct 09 '09 20:10

Yoo


1 Answers

The two defalias usages are slightly different. The first links the function cell for 'backward-delete-char to that of 'delete-backward-char. The second links the 'search-forward-regexp to the function that is currently called by 're-search-forward.

The difference is that if you later change the definition of 'delete-backward-char, 'backward-delete-char will now have the new behavior. Whereas in the second case, changing the function for 're-search-forward has no effect on the behavior of 'search-forward-regexp.

Perhaps some ascii art can help:

+-------------------------+     +-----------------+ |#<subr re-search-forward>| <-- |re-search-forward| +-------------------------+     +-----------------+                         ^       +---------------------+                         \------ |search-forward-regexp|                                 +---------------------+  +----------------------------+     +--------------------+     +--------------------+ |#<subr delete-backward-char>| <-- |delete-backward-char| <-- |backward-delete-char| +----------------------------+     +--------------------+     +--------------------+ 

This documentation might help clear things up.

like image 174
Trey Jackson Avatar answered Sep 30 '22 00:09

Trey Jackson