如何查找Racket模块中所有功能的列表?

时间:2014-07-31 15:48:46

标签: module racket

我知道我可以阅读文档,但是因为这需要将我的注意力从我的编辑器和REPL转移开来,所以我希望能够看到我正在使用的模块提供的功能列表。

Racket中有什么东西类似于Ruby Foo.methods()

2 个答案:

答案 0 :(得分:4)

函数module->exports提供了模块provide表达式中的名称列表。

> (require racket/date)
> (module->exports 'racket/date) ;module name is passed not the module
'()
'((0
   (julian/scalinger->string ())
   (date->julian/scalinger ())
   (find-seconds ())
   (date-display-format ())
   (date->string ())
   (date*->seconds ())
   (date->seconds ())
   (current-date ())))

参考:racket discussion list

由于module->exports返回两个值,因此需要define-values来捕获函数列表和用于其他地方的宏列表:

; my-module.rkt
#lang racket 
(provide (all-defined-out))
(define-syntax while
  #|
    Macros do not allow documentation strings
    While macro from StackOverflow.
    http://stackoverflow.com/questions/10968212/while-loop-macro-in-drracket
    |#
  (syntax-rules ()
    ((_ pred? stmt ...)
     (do () ((not pred?))
       stmt ...))))

(define my-const 9)

(define (prn a)
"Prints the passed value on a new line"
  (printf "\n~a" a))

(define (my-func a)
"Another documentation string"
  (prn a))

参考:

;some-other-file.rkt
#lang racket
(require "my-module.rkt")
(define-values (functions-and-constants macros)
  (module->exports '"my-module.rkt"))

在REPL中:

> functions-and-constants
'((0 (my-const ()) (my-func ()) (prn ())))
> macros
'((0 (while ())))

答案 1 :(得分:1)

如果您使用xrepl,则可以使用describe命令获取更有用的格式化信息:

Welcome to Racket v6.1.0.3.
-> ,describe racket/date
; `racket/date' is a module,
;   located at <collects>/racket/date.rkt
;   imports: <collects>/racket/base.rkt, <collects>/racket/contract/base.rkt,
;     <collects>/racket/promise.rkt.
;   direct syntax exports: current-date, date*->seconds,
;     date->julian/scalinger, date->seconds, date->string, date-display-format,
;     find-seconds, julian/scalinger->string.