Skip to content

Instantly share code, notes, and snippets.

@nyuichi
Last active August 29, 2015 14:02
Show Gist options
  • Save nyuichi/88a46609ddb3ea03a0d7 to your computer and use it in GitHub Desktop.
Save nyuichi/88a46609ddb3ea03a0d7 to your computer and use it in GitHub Desktop.
; たとえばこんなライブラリがあったとして…
(define-library (my lib)
(import (scheme base))
(define foo 1)
(define bar 2)
(export foo)
(export (rename bar baz)))
; このライブラリを値として動的に取り出してみます。
(define my-lib (library '(my lib)))
; ライブラリはそれぞれexport tableというものを持っていて、それがどのシンボルをエクスポートするかのテーブルになっています。
(define my-lib-exports (library-exports my-lib))
my-lib-exports
=> #H((foo . foo@245) (baz . bar@246))
; それを動的に書き換えてみます。
(hash-table-put! my-lib-exports 'qux (hash-table-get my-lib-exports 'foo))
my-lib-exports
=> #H((foo . foo@245) (baz . bar@256) (qux . foo@245))
; ここで(my lib)をimportしてみましょう。テーブルが書き換えられているので、quxもインポートされます。quxの実態は、(my lib)の中のfoo変数です。
(import (my lib))
foo (~~> foo@245) => 1
bar (~~> not found) => error
baz (~~> bar@256) => 2
qux (~~> foo@245) => 1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment