unofficial mirror of bug-gnu-emacs@gnu.org 
 help / color / mirror / code / Atom feed
* bug#22961: The read function does not support a lambda argument
@ 2016-03-09  8:37 Mihai Călin Bazon
  2016-03-09 18:27 ` John Mastro
                   ` (2 more replies)
  0 siblings, 3 replies; 5+ messages in thread
From: Mihai Călin Bazon @ 2016-03-09  8:37 UTC (permalink / raw)
  To: 22961

[-- Attachment #1: Type: text/plain, Size: 705 bytes --]

Example, I eval this in the scratch buffer (with lexical-binding set true):

    (read (let ((str "TEST")
                (pos 0)
                (prev nil))
            (lambda (ch)
              (cond
                (ch (push ch prev))
                (prev (pop prev))
                ((< pos (length str))
                 (prog1 (aref str pos)
                   (setq pos (1+ pos))))))))

According to the docs [1] an input stream can be a function.  I would
expect the above to return the symbol TEST, but instead I get an error
("end of file during parsing").

[1]
http://www.gnu.org/software/emacs/manual/html_node/elisp/Input-Streams.html#Input-Streams

-- 
Mihai Bazon,
http://lisperator.net/

[-- Attachment #2: Type: text/html, Size: 1183 bytes --]

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

* bug#22961: The read function does not support a lambda argument
  2016-03-09  8:37 bug#22961: The read function does not support a lambda argument Mihai Călin Bazon
@ 2016-03-09 18:27 ` John Mastro
  2016-03-09 18:46 ` Andreas Schwab
  2016-03-09 20:31 ` Kaushal Modi
  2 siblings, 0 replies; 5+ messages in thread
From: John Mastro @ 2016-03-09 18:27 UTC (permalink / raw)
  To: 22961; +Cc: Mihai Călin Bazon

Mihai Călin Bazon <mihai.bazon@gmail.com> wrote:
> Example, I eval this in the scratch buffer (with lexical-binding set true):
>
>     (read (let ((str "TEST")
>                 (pos 0)
>                 (prev nil))
>             (lambda (ch)
>               (cond
>                 (ch (push ch prev))
>                 (prev (pop prev))
>                 ((< pos (length str))
>                  (prog1 (aref str pos)
>                    (setq pos (1+ pos))))))))
>
> According to the docs [1] an input stream can be a function.  I would expect
> the above to return the symbol TEST, but instead I get an error ("end of
> file during parsing").

Confirmed on Emacs 25. However, it does work with a named defun:

    (let ((str "TEST")
          (pos 0)
          (prev nil))
      (defun my-read-function (&optional ch)
        (cond (ch (push ch prev))
              (prev (pop prev))
              ((< pos (length str))
               (prog1 (elt str pos)
                 (setq pos (1+ pos)))))))

    (read #'my-read-function) ;=> TEST

(Mihai, the ‘ch’ argument should be ‘&optional’, though that's not
related to the error in question).

-- 
john





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

* bug#22961: The read function does not support a lambda argument
  2016-03-09  8:37 bug#22961: The read function does not support a lambda argument Mihai Călin Bazon
  2016-03-09 18:27 ` John Mastro
@ 2016-03-09 18:46 ` Andreas Schwab
  2016-03-09 20:31 ` Kaushal Modi
  2 siblings, 0 replies; 5+ messages in thread
From: Andreas Schwab @ 2016-03-09 18:46 UTC (permalink / raw)
  To: Mihai Călin Bazon; +Cc: 22961-done

Should be fixed in commit 711ca36.

Andreas.

-- 
Andreas Schwab, schwab@linux-m68k.org
GPG Key fingerprint = 58CA 54C7 6D53 942B 1756  01D3 44D5 214B 8276 4ED5
"And now for something completely different."





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

* bug#22961: The read function does not support a lambda argument
  2016-03-09  8:37 bug#22961: The read function does not support a lambda argument Mihai Călin Bazon
  2016-03-09 18:27 ` John Mastro
  2016-03-09 18:46 ` Andreas Schwab
@ 2016-03-09 20:31 ` Kaushal Modi
  2016-03-09 21:10   ` Mihai Călin Bazon
  2 siblings, 1 reply; 5+ messages in thread
From: Kaushal Modi @ 2016-03-09 20:31 UTC (permalink / raw)
  To: 22961, mihai.bazon; +Cc: John Mastro, Andreas Schwab

[-- Attachment #1: Type: text/plain, Size: 595 bytes --]

I just re-built using this commit (
http://git.savannah.gnu.org/cgit/emacs.git/commit/?h=emacs-25&id=711ca362e7f8ca7c4f664dc2fe60bc5fa4e4f4fe
).

Now the earlier error went away, but I get

  Symbol’s value as variable is void: prev

I guess the read call should be inside the let:

(let ((str "TEST")
      (pos 0)
      (prev nil))
  (read (lambda (&optional ch)
          (cond
           (ch (push ch prev))
           (prev (pop prev))
           ((< pos (length str))
            (prog1 (aref str pos)
              (setq pos (1+ pos))))))))

Above returns "TEST".

[-- Attachment #2: Type: text/html, Size: 1032 bytes --]

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

* bug#22961: The read function does not support a lambda argument
  2016-03-09 20:31 ` Kaushal Modi
@ 2016-03-09 21:10   ` Mihai Călin Bazon
  0 siblings, 0 replies; 5+ messages in thread
From: Mihai Călin Bazon @ 2016-03-09 21:10 UTC (permalink / raw)
  To: Kaushal Modi; +Cc: John Mastro, Andreas Schwab, 22961

[-- Attachment #1: Type: text/plain, Size: 1071 bytes --]

You should (setq lexical-binding t) before running it (and note that's
buffer-local, I've been bitten by that several times).  With lexical
binding my original example should work (with the fix, as John pointed out,
that the argument should be &optional).

Thanks Andreas for the quick fix! :-)

On Wed, Mar 9, 2016 at 10:31 PM, Kaushal Modi <kaushal.modi@gmail.com>
wrote:

> I just re-built using this commit (
> http://git.savannah.gnu.org/cgit/emacs.git/commit/?h=emacs-25&id=711ca362e7f8ca7c4f664dc2fe60bc5fa4e4f4fe
> ).
>
> Now the earlier error went away, but I get
>
>   Symbol’s value as variable is void: prev
>
> I guess the read call should be inside the let:
>
> (let ((str "TEST")
>       (pos 0)
>       (prev nil))
>   (read (lambda (&optional ch)
>           (cond
>            (ch (push ch prev))
>            (prev (pop prev))
>            ((< pos (length str))
>             (prog1 (aref str pos)
>               (setq pos (1+ pos))))))))
>
> Above returns "TEST".
>



-- 
Mihai Bazon,
http://lisperator.net/

[-- Attachment #2: Type: text/html, Size: 1920 bytes --]

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

end of thread, other threads:[~2016-03-09 21:10 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2016-03-09  8:37 bug#22961: The read function does not support a lambda argument Mihai Călin Bazon
2016-03-09 18:27 ` John Mastro
2016-03-09 18:46 ` Andreas Schwab
2016-03-09 20:31 ` Kaushal Modi
2016-03-09 21:10   ` Mihai Călin Bazon

Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).