如何在球拍模块中找到所有功能的列表?

ben*_*ers 5 module racket

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

Racket中有和Ruby类似的东西Foo.methods()吗?

ben*_*ers 5

该函数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 ())))
Run Code Online (Sandbox Code Playgroud)

参考:球拍讨论表

因为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))
Run Code Online (Sandbox Code Playgroud)

引用于:

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

在REPL中:

> functions-and-constants
'((0 (my-const ()) (my-func ()) (prn ())))
> macros
'((0 (while ())))
Run Code Online (Sandbox Code Playgroud)