all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
From: Andy Wingo <wingo@pobox.com>
To: Daniel Colascione <dan.colascione@gmail.com>
Cc: Helmut Eller <eller.helmut@gmail.com>, emacs-devel@gnu.org
Subject: Re: Keyword args
Date: Mon, 13 Dec 2010 21:00:47 +0100	[thread overview]
Message-ID: <m3bp4pjvdc.fsf@unquote.localdomain> (raw)
In-Reply-To: <4D0580A6.7090307@gmail.com> (Daniel Colascione's message of "Sun, 12 Dec 2010 18:10:46 -0800")

On Mon 13 Dec 2010 03:10, Daniel Colascione <dan.colascione@gmail.com> writes:

> Clearly, the solution is more uniform keyword argument parsing; either
> library functions in C could be provided, or make-network-process could
> be made a Lisp keyword-parsing front-end for some horrible
> %make-network-process that implements the functionality.

FWIW, Guile supports keyword arguments natively. IMO the proper way to
do things is to keep a uniform calling convention, and allow procedures
to parse arguments themselves, with low-level support.

    scheme@(guile-user)> (lambda* (#:key (foo 42)) foo)
    $1 = #<procedure 1c2f080 at standard input:1:0 (#:key foo)>
    scheme@(guile-user)> ,disassemble $1
    Disassembly of #<procedure 1c2f080 at standard input:1:0 (#:key foo)>:

Here we have some instructions that aren't disassembled quite as
perspicaciously as one might like, but they take the args on the stack,
and shuffle the non-positional args up:

       0    (assert-nargs-ge 0 0)           
       3    (bind-optionals/shuffle 0 0 0 0 0 1)

And here we bind keywords. This says "fetch the keywords from the
constant table at index 1, and scan the non-positional args for one
keyword, disallowing other keywords.

      10    (bind-kwargs 0 1 0 1 0)         

It's somewhat complicated code, but it's a const only borne by keyword
arguments. Here we have the code that initializes `foo' if it's not
given:

      16    (reserve-locals 0 1)            
      19    (local-bound? 0)                
      21    (br-if :L111)                   ;; -> 29
      25    (make-int8 42)                  ;; 42
      27    (local-set 0)                   ;; `foo'

And finally (!) the main body:

      29    (local-ref 0)                   ;; `foo'
      31    (return)                        

Some tests:

    > (define (fib n) (if (< n 2) 1 (+ (fib (- n 1)) (fib (- n 2)))))
    > ,time (fib 35)
    $1 = 14930352
    clock utime stime cutime cstime gctime
     2.99  2.99  0.01   0.00   0.00   0.00
    > (define* (fibk #:key (n 0)) (if (< n 2) 1 (+ (fibk #:n (- n 1)) (fibk #:n (- n 2)))))
    > ,time (fibk 35)
    <stdin>:5:6: warning: possibly wrong number of arguments to `fibk'
    While executing meta-command:
    ERROR: Odd length of keyword argument list
    > ,time (fibk #:n 35)
    $2 = 14930352
    clock utime stime cutime cstime gctime
     5.01  4.99  0.00   0.00   0.00   0.00

FWIW on this machine a byte-compiled elisp (fib 35) on emacs takes about
6 seconds.

I wrote more about this sort of thing here:

    http://wingolog.org/archives/2009/11/07/case-lambda-in-guile
    http://wingolog.org/archives/2009/11/08/optionals-keywords-oh-my

Our elisp support uses this native infrastructure for keywords and
optionals, but there obviously are some differences regarding
implementation of dynamic scope.  Lexical binding is a lot cheaper, for
Guile, but we hope to get dynamic binding cheap too.

Anyway, I would like to discourage complicated implementations in
"user-space" for keyword arguments. They should be a core language
feature, for all the reasons I gave in my first article.

Happy hacking,

Andy
-- 
http://wingolog.org/



  parent reply	other threads:[~2010-12-13 20:00 UTC|newest]

Thread overview: 44+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2010-12-05 23:55 Return MON KEY
2010-12-06  1:48 ` Return Stephen J. Turnbull
2010-12-06  5:50   ` Return MON KEY
2010-12-06  7:20     ` Return Stephen J. Turnbull
2010-12-06  9:00       ` Return David Kastrup
2010-12-06 19:11         ` Return Stefan Monnier
2010-12-06 19:09       ` Return Stefan Monnier
2010-12-06 19:19         ` Return Chong Yidong
2010-12-06 20:27           ` Return Stefan Monnier
2010-12-07  4:47         ` Return Miles Bader
2010-12-07  9:17           ` Return David Kastrup
2010-12-07 17:10             ` Return Stefan Monnier
2010-12-07 22:15               ` Return David Kastrup
2010-12-08 15:50             ` Return Fren Zeee
2010-12-09 22:38             ` Return Stefan Monnier
2010-12-10  1:41               ` Return Stephen J. Turnbull
2010-12-10  3:44                 ` Return Stefan Monnier
2010-12-10  8:28                   ` Return Stephen J. Turnbull
2010-12-23  5:39                     ` Return Fren Zeee
2010-12-07 12:44         ` Return Stephen J. Turnbull
2010-12-07 14:38           ` Return David Kastrup
2010-12-07 16:14             ` Return Stephen J. Turnbull
2010-12-07 17:11               ` Return tomas
2010-12-07  2:42       ` Return MON KEY
2010-12-07 14:34         ` Return Stephen J. Turnbull
2010-12-07 15:54           ` Return David Kastrup
2010-12-07 16:30             ` Return Stephen J. Turnbull
2010-12-08 13:42               ` Return Richard Stallman
2010-12-10  7:42               ` Return Daniel Colascione
2010-12-10  8:53                 ` Keyword args (was: Return) Helmut Eller
2010-12-13  2:10                   ` Keyword args Daniel Colascione
2010-12-13  8:30                     ` Helmut Eller
2010-12-13 20:00                     ` Andy Wingo [this message]
2010-12-14  5:03                       ` Miles Bader
2010-12-14  7:43                         ` Helmut Eller
2010-12-07 22:55           ` Return MON KEY
2010-12-08  7:28             ` Return Stephen J. Turnbull
2010-12-08 18:11               ` Return MON KEY
2010-12-09  8:37                 ` Return Stephen J. Turnbull
2010-12-07 23:21         ` Return Samuel Bronson
2010-12-08  8:06           ` Return Stephen J. Turnbull
2010-12-08 20:51             ` Return Samuel Bronson
  -- strict thread matches above, loose matches on Subject: below --
2010-12-12  4:49 Keyword args (was: Return) MON KEY
2010-12-12  8:34 ` Keyword args Helmut Eller
2010-12-12 17:01 MON KEY

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=m3bp4pjvdc.fsf@unquote.localdomain \
    --to=wingo@pobox.com \
    --cc=dan.colascione@gmail.com \
    --cc=eller.helmut@gmail.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.