From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: Paul Pogonyshev Newsgroups: gmane.emacs.devel Subject: Re: Arbitrary function: find the number(s) of expected arguments Date: Wed, 16 Mar 2016 08:41:18 +0100 Message-ID: References: <56E8906C.5050405@lanl.gov> NNTP-Posting-Host: plane.gmane.org Mime-Version: 1.0 Content-Type: multipart/alternative; boundary=001a113b967eb379aa052e25a33f X-Trace: ger.gmane.org 1458114096 31230 80.91.229.3 (16 Mar 2016 07:41:36 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Wed, 16 Mar 2016 07:41:36 +0000 (UTC) Cc: emacs-devel@gnu.org To: Davis Herring Original-X-From: emacs-devel-bounces+ged-emacs-devel=m.gmane.org@gnu.org Wed Mar 16 08:41:28 2016 Return-path: Envelope-to: ged-emacs-devel@m.gmane.org Original-Received: from lists.gnu.org ([208.118.235.17]) by plane.gmane.org with esmtp (Exim 4.69) (envelope-from ) id 1ag65A-0008JG-4l for ged-emacs-devel@m.gmane.org; Wed, 16 Mar 2016 08:41:28 +0100 Original-Received: from localhost ([::1]:53692 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ag656-0008Bq-DP for ged-emacs-devel@m.gmane.org; Wed, 16 Mar 2016 03:41:24 -0400 Original-Received: from eggs.gnu.org ([2001:4830:134:3::10]:37143) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ag652-0008Bb-HK for emacs-devel@gnu.org; Wed, 16 Mar 2016 03:41:21 -0400 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1ag651-0001MK-Dp for emacs-devel@gnu.org; Wed, 16 Mar 2016 03:41:20 -0400 Original-Received: from mail-oi0-x236.google.com ([2607:f8b0:4003:c06::236]:34455) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ag651-0001Lo-6C for emacs-devel@gnu.org; Wed, 16 Mar 2016 03:41:19 -0400 Original-Received: by mail-oi0-x236.google.com with SMTP id m82so32319382oif.1 for ; Wed, 16 Mar 2016 00:41:19 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :cc; bh=E/QE0DLcLd9v+zhIe2ap4byZNFWL2PZFfdV0IEhr69U=; b=Jyb8S7VBCilXqNepSc0flkTKMCGcQ0ED84d2nlSASqD67XHXC6KSVzp9aS4CPmn00t fVcMpLngkNffbIlJlVsjuOsMjFz+W6//LBkwJSxOCZNT0w9v7Cb2YNGakoDXM2+uOEpK +jnJ8TveWQa4vTpt6oFfwAd9hqMThl8lSp8X3CO8p2npmzpmiPQnBv4fQAYzjYaqf58R PAD/jKHwzUSWkjw2PUt1DxpqJC2xSzTpKIRb8nNMQZNpNlrgc125J598y5nUIU8Tg73y OPGTDNZD1Ma8S4AIExtEnElDLd1tGRNNeulzqXGHsAvDpp5dzGkc9/plTNMNkAKk+wLx sTNg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:mime-version:in-reply-to:references:date :message-id:subject:from:to:cc; bh=E/QE0DLcLd9v+zhIe2ap4byZNFWL2PZFfdV0IEhr69U=; b=M9ylCKL31lEf5KfLDBxd8LAxPdb0HcckueMbDK63sMRL0xxR2WRYCCpSEq/Rh2nubM Bh7OL5fgjvWdLlBXywo9585N+XCUhDCjSfIXf5p/VfRo3PTY0S1TLd2d6XA0QfxHVdMM nGJgaYWnwWz9T89RYFApYjLaU8IAeqFVBbsRnzN+ujdSBUzva79vAUhi/t50GHsv28VG vI0DtadA4V/LWvApWC9gcsImnAcP7dq9156hV6ZapeuISmVLoSERrJKgBs1m4MseihdY 5aiGIQeLQsuuzZe/WxoOlC9K1Zb39hiJcChQdRPXWIsipN2Y2hfBSyGN9AoYwxhP8j0h CDDQ== X-Gm-Message-State: AD7BkJImYTwdBWKeUAm73uFm+NTwoa06qnUJdj8Bozn/s4EyqbBU/mvgNLtJmQeq7gX7lgHt6STW0cnFUym2vA== X-Received: by 10.202.3.135 with SMTP id 129mr1305847oid.91.1458114078666; Wed, 16 Mar 2016 00:41:18 -0700 (PDT) Original-Received: by 10.202.71.7 with HTTP; Wed, 16 Mar 2016 00:41:18 -0700 (PDT) In-Reply-To: <56E8906C.5050405@lanl.gov> X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 2607:f8b0:4003:c06::236 X-BeenThere: emacs-devel@gnu.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: "Emacs development discussions." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: emacs-devel-bounces+ged-emacs-devel=m.gmane.org@gnu.org Original-Sender: emacs-devel-bounces+ged-emacs-devel=m.gmane.org@gnu.org Xref: news.gmane.org gmane.emacs.devel:201795 Archived-At: --001a113b967eb379aa052e25a33f Content-Type: text/plain; charset=UTF-8 That's a fair point, but frankly, it was just an example of why one might want to use such a function to find arity of arbitrary callable. In this example I could also invoke callback with two arguments only if it wants _at least_ two. I.e. (my-foo (lambda (x &optional y) ...)) ==> called back with one argument but (my-foo (lambda (x y) ...)) ==> called back with two arguments Another usecase is possibility to fail early. I know that's not a popular concept in Elisp, but I find it very useful. The idea is, when you are given some value that you use only later, you validate it right away. Thus, you can give immediate error with explanation rather than letting it fail in mysterious and hard to understand way in a completely different place later: (defun my-set-termination-callback (callback) (unless (and (functionp callback) (>= (car (function-arity callback)) 1)) (error ...)) (setq my-termination-callback callback)) Paul On 15 March 2016 at 23:45, Davis Herring wrote: > This way `my-foo' automagically accepts callbacks that expect one argument >> (as in initial form), as well as those that want two arguments. >> > > What if your existing clients might have > > (defun paul-callback (computed-data &optional cached) > ...) > > where they make calls to `paul-callback' themselves with the extra > argument? It's fair to call this "abstruse" (maybe most/all clients just > use a lambda), but it might also make you think about providing another > callback interface. > > Davis > > -- > This product is sold by volume, not by mass. If it appears too dense or > too sparse, it is because mass-energy conversion has occurred during > shipping. > --001a113b967eb379aa052e25a33f Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: quoted-printable
That's a fair point, but frankly, it was just an = example of why one might want to use such a function to find arity of arbit= rary callable. In this example I could also invoke callback with two argume= nts only if it wants _at least_ two. I.e.

=C2=A0=C2=A0=C2= =A0 (my-foo (lambda (x &optional y) ...)) =3D=3D> called back with o= ne argument

but

=C2=A0=C2=A0=C2=A0 (my-= foo (lambda (x y) ...)) =3D=3D> called back with two arguments

Another usecase is possibility to fail early. I know that's no= t a popular concept in Elisp, but I find it very useful. The idea is, when = you are given some value that you use only later, you validate it right awa= y. Thus, you can give immediate error with explanation rather than letting = it fail in mysterious and hard to understand way in a completely different = place later:

=C2=A0=C2=A0=C2=A0 (defun my-set-termination= -callback (callback)
=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 (unless (= and (functionp callback) (>=3D (car (function-arity callback)) 1))
=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 (error ...))
<= div>=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 (setq my-termination-callback callback))=

Paul

On 15 March 2016 at 23:45, Davi= s Herring <herring@lanl.gov> wrote:
This way `my-foo' automagically accepts callbacks that expect one argum= ent
(as in initial form), as well as those that want two arguments.

What if your existing clients might have

(defun paul-callback (computed-data &optional cached)
=C2=A0 ...)

where they make calls to `paul-callback' themselves with the extra argu= ment?=C2=A0 It's fair to call this "abstruse" (maybe most/all= clients just use a lambda), but it might also make you think about providi= ng another callback interface.

Davis

--
This product is sold by volume, not by mass.=C2=A0 If it appears too dense = or too sparse, it is because mass-energy conversion has occurred during shi= pping.

--001a113b967eb379aa052e25a33f--