Skip to content

Instantly share code, notes, and snippets.

@ijp
Created October 27, 2011 23:21
Show Gist options
  • Save ijp/1321182 to your computer and use it in GitHub Desktop.
Save ijp/1321182 to your computer and use it in GitHub Desktop.
(define guardian (make-guardian))
(define finalizer-table (make-weak-key-hash-table))
(let ((f (lambda () (display "test\n"))))
(guardian f)
(hashq-set! finalizer-table (cons #f #f) f)
#f) ;; just in case you want to test at repl
(write finalizer-table)
(newline)
(gc)(gc)(gc)(gc)(gc)(gc)(gc)(gc)(gc)(gc)(gc)(gc) ;; surely a dozen should do?
(write finalizer-table)
(newline)
(write (guardian))
(write (guardian))
(write (guardian))
(write (guardian))
(gc)
(write (guardian))
(write (guardian))
(write (guardian))
(newline)
@ijp
Copy link
Author

ijp commented Oct 27, 2011

You'd think at least one of those (write (guardians)) (quadrupled for dramatic effect) would display something other than #f.

[ian@Kagami tmp]$ guile -s weakhash_vs_guardian.scm 
;;; note: source file /tmp/weakhash_vs_guardian.scm
;;;       newer than compiled /home/ian/.cache/guile/ccache/2.0−LE−4−2.0/tmp/weakhash_vs_guardian.scm.go
;;; note: auto−compilation is enabled, set GUILE_AUTO_COMPILE=0
;;;       or pass the −−no−auto−compile argument to disable.
;;; compiling /tmp/weakhash_vs_guardian.scm
;;; compiled /home/ian/.cache/guile/ccache/2.0−LE−4−2.0/tmp/weakhash_vs_guardian.scm.go
#<weak−key−hash−table 1/31>
#<weak−key−hash−table 0/31>
#f#f#f#f#f#f#f
[ian@Kagami tmp]$ 

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment