如何在 Racket 中提供与结构相关的所有功能 [英] How to provide all functions associated with a struct in Racket

查看:64
本文介绍了如何在 Racket 中提供与结构相关的所有功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有一种方法可以提供与 struct 关联的所有函数,而无需明确枚举它们?

Is there a way to provide all of the functions associated with a struct without explicitly enumerating them?

例如,如果我有 foo.rkt:

#lang racket
(provide foo-struct) ; provide all functions, not just the constructor
(struct foo-struct (biz bop))

然后bar.rkt:

#lang racket
(require "foo.rkt")
(define foo (foo-struct 1 2)) ; works just fine
(foo-struct-biz foo) ; is undefined

有没有办法提供 foo-struct-bizfoo-struct-bop(以及与结构相关的任何其他方法)而不明确列出它们?

Is there a way to provide foo-struct-biz and foo-struct-bop (and any other methods associated with the struct) without explicitly listing them?

推荐答案

感谢@PetSerAl 在评论中,这样做的方法是

Thanks to @PetSerAl in the comments, the way to do this is

(provide (struct-out foo-struct))

它将提供与 foo-struct 相关的所有方法.

which will provide all of the methods associated with foo-struct.

这篇关于如何在 Racket 中提供与结构相关的所有功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆