dash.el icon indicating copy to clipboard operation
dash.el copied to clipboard

Add `-defun'

Open nbfalcon opened this issue 4 years ago • 12 comments

-defun is like -lambda, but with destructuring. To implement it, abstract the arglist and form generation code of -lambda into separate functions.

Additional benefits of the refactoring are:

  • -lambda (and by extension, -defun) can now accept empty argument lists
  • -lambda (and -defun) no longer rebinds symbols (fixing the TODO)

Various declaration forms should be handled properly in -defun.

Motivation: when this is merged, lsp-defun' can be made an alias for -defun`.

  • Please note the use of intern instead of make-symbol. Using the latter instead caused test-failures.

  • What git-commit-major-mode do you prefer? I assumed and used git-commit-elisp-text-mode here

  • [x] add examples

nbfalcon avatar Nov 06 '20 09:11 nbfalcon

I haven't noticed yyoncho's PR... This is going to be awkward.

nbfalcon avatar Nov 06 '20 09:11 nbfalcon

I believe my PR to be better: it has no code duplication, and it doesn't rebind symbols, so things like (-lamba (foo)) are the same as (lambda (foo)). Is signing a copyright disclaimer enough, and if so, how would I go about doing that?

nbfalcon avatar Nov 06 '20 10:11 nbfalcon

I'll add support for &optional.

nbfalcon avatar Nov 06 '20 10:11 nbfalcon

Is signing a copyright disclaimer enough

Yes.

and if so, how would I go about doing that?

Just email the completed form at https://git.savannah.gnu.org/cgit/gnulib.git/tree/doc/Copyright/request-assign.future to [email protected]; they'll give you further instructions. It's generally a matter of scanning and emailing a scanned document, or in some countries digitally signing a document. See (info "(emacs) Copyright Assignment") for more.

Thanks.

basil-conto avatar Nov 06 '20 11:11 basil-conto

Now we also support &rest and &optional.

nbfalcon avatar Nov 06 '20 11:11 nbfalcon

TODO: add &key, making -defun a 100% drop-in replacement for cl-defun.

nbfalcon avatar Nov 06 '20 11:11 nbfalcon

Now we have -defmacro, and correct function signatures :).

Also, do you think I should implicitly add a signature doc to -lambda, if a docstring is provided? What we have currently:

(-lambda (a b)) = (lambda (a b))
(-lambda (a b) "Some docstring...") = (lambda (a b) "Some docstring...") ; no destructuring and signature comment necessary, as we only use symbols in the arglist
(-lambda ((a b)) "") = (-lambda ((a b)) "\n\n(fn ((a b)))") ; Note the auto-generated signature docstring fragment
(-lambda ((a b))) = (-lambda ((a b))) ; no signature docstring, as the user probably doesn't care if they didn't bother to provide a docstring

What I could do instead is to never touch -lambda docstrings at all. What do you think?

Other than &key (which may or may not be needed), this PR is ready.

nbfalcon avatar Nov 06 '20 13:11 nbfalcon

I couldn't write a proper edebug-form-spec for -defun, -lambda, -defmacro's lambda-lists, because I still don't understand them.

nbfalcon avatar Nov 06 '20 14:11 nbfalcon

Now I added:

  • (-lambda [sym &as &DocumentSymbol :kind]) = (-lambda ((sym &as &DocumentSymbol :kind)))
  • (-lambda ((sym &as &DocumentSymbol :kind)) = (lambda (sym) (-let (((&DocumentSymbol :kind) sym))) (&as is destructured directly, without going trough inputX)

nbfalcon avatar Nov 06 '20 15:11 nbfalcon

My changes seem to not have broken anything: I am using my modified dash.el with lsp-mode, a heavy user of -lambda, without any issues. I have recompiled the former a few times already.

nbfalcon avatar Nov 08 '20 12:11 nbfalcon

Thanks, I'll have a closer look in the coming days. Let us know how the copyright assignment process goes.

basil-conto avatar Nov 08 '20 13:11 basil-conto

I have sent the E-Mail for the copyright assignment.

nbfalcon avatar Nov 08 '20 15:11 nbfalcon