gpt4 book ai didi

scheme - Racket 中的结构有命名约定吗?

转载 作者:行者123 更新时间:2023-12-01 16:53:34 25 4
gpt4 key购买 nike

我喜欢使用 % 后缀命名类的约定,因为它有助于将实例与高阶类区分开来。

(define ingredient%
(class object%
(init-field name taste price color)
(super-new)))

(define (total-price ingredients)
(for/sum (ingredient ingredients)
;; here "ingredient" stands for the instance, not the class ingredient%
(get-field price ingredient)))

另一方面,当我需要一个结构时,我正在努力寻找不与结构名称冲突的实例的名称。

(struct ingredient (name taste price color))

(define (total-price ingredients)
(for/sum (igrdt ingredients)
;; igrdt is definitely a bad name
(ingredient-price igrdt)))

;; it looks even worse in arguments
(define (taste igrdt)
(match (ingredient-taste igrdt)
(('good) "It's good!")
(('bad) "Yuk...")
(else "It's something.")))

所以我也开始为结构使用后缀:§。该符号在我的键盘上很容易访问,可惜可能不是标准的 QWERTY 键盘,所以我想我稍后会将其更改为 $

(struct ingredient§ (name taste price color))
(define ingredient (ingredient§ "Carrot" 'good 1.0 'orange))
(displayln (format "I'm making a ~a cake." (ingredient§-name ingredient)))

无论是什么符号,我发现对结构使用后缀可以让您自由选择清晰的实例名称,而不必从变量中删除随机元音。

另一种选择是使用 % 作为结构的前缀和类的后缀,以便结构访问器保持可读性:(%ingredient-name成分) .

从 GitHub 的快速研究来看: https://github.com/search?p=1&q=struct-out+language%3Aracket&type=Code

看起来每个人都在按原样使用结构(除了少数学生使用 CamelCase,因为他们已经习惯了)。

那么我可以做些什么来区分实例和结构类型呢?它甚至相关吗?我想得远吗?我应该在软件工程上问这个吗?

最佳答案

命名约定是 struct 没有特殊符号。这可能是由于 struct 比类、单元、签名、组件等更“类似于方案”,但我可能是错的。

struct 发明类似于类、单元等的东西并没有错。但它是任意的和个人的,因此可能应该记录在案。

关于scheme - Racket 中的结构有命名约定吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47573893/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com