Summary
variables defined inside closure survive beyond closure!
Metadata
- Id: 118cf095522e7c697abc9b736a092e8cece4fcdc
- Trac id: 1297
- Type: change request
- Reporter: ai-artisan
- Owner:
- Cc:
- Status: closed
- Component: core libraries
- Estimated difficulty:
- Resolution: duplicate
- Priority: not urgent at all
- Milestone: someday
- Version: 4.11.0
- Changetime: 2016-08-02 20:35:52 UTC
- Created: 2016-06-09 04:23:38 UTC
- Keywords: closure scope begin
Attachments
- 118cf095522e7c697abc9b736a092e8cece4fcdc/attachments/debug.scm
Description
- ;debug.scm
(require-extension r7rs)
(define r #t)
(define test (lambda ()
(if r (begin (set! r #f) (define v 123) (test) (display v) (newline) ) (begin (define v 234) ) )
))
(test)
- ; output
- 234
- ; The question is
- Each time a procedure or partial block (like "let") is called, a closure should be created. Variables defined inside a closure should have a limited scope inside the closure unless "call/cc" or "call-with-values" is used. In the case above, none of them is used but the variable "v" survives beyond its closure created by "test" the 2nd time.
- ; Further more, I found that only when "begin" is used will this bug appear.
Changes and comments
[2016-06-09 04:28:13 UTC] ai-artisan attached debug.scm (description=csi -w -s debug.scm)
[2016-06-09 04:39:21 UTC] evhan changed priority from critical to minor
[2016-06-09 04:39:21 UTC] evhan wrote:
Internal definitions are only valid at the start of a body (see R5RS). You have definitions in invalid locations (begin does not open a body), which CHICKEN allows but treats as global assignments.
The following, where internal definitions are correctly placed, prints 123 as you expect:
(define test
(lambda ()
(if r
(begin
(set! r #f)
(let ()
(define v 123)
(test)
(display v)
(newline)))
(let ()
(define v 234)))))
[2016-06-09 10:43:48 UTC] ai-artisan wrote:
Thx. I just referred to the R7RS. You r right. I made a mistake. ^_^
[2016-06-09 11:01:45 UTC] ai-artisan changed priority from minor to not urgent at all
[2016-08-02 20:35:52 UTC] sjamaan changed status from new to closed
[2016-08-02 20:35:52 UTC] sjamaan set resolution to duplicate
[2016-08-02 20:35:52 UTC] sjamaan wrote:
This is exactly the same underlying issue as #1309