Scheme Racket中的结构是否有命名约定?
我喜欢这样一个事实,即使用Scheme Racket中的结构是否有命名约定?,scheme,racket,Scheme,Racket,我喜欢这样一个事实,即使用%后缀命名类是一种惯例,因为它有助于区分实例和高阶类 (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 inst
%
后缀命名类是一种惯例,因为它有助于区分实例和高阶类
(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)))
无论符号是什么,我发现为结构使用后缀可以让您自由选择清晰的实例名称,而不必从变量中删除随机元音
另一种选择是使用%
作为结构的前缀和类的后缀,以便结构访问器保持可读性:(%component-name-component)
通过快速的GitHub研究判断:
看起来每个人都在按原样使用structs(除了少数学生使用CamelCase,因为他们已经习惯了)
那么,如何区分实例和结构类型呢?
这有关系吗?我想走远吗?关于软件工程,我应该问这个问题吗?命名惯例是
struct
s没有特殊的符号。这可能是因为struct
比类、单元、签名、组件等更像“scheme”,但我可能错了
为
struct
发明类似于类、单位等的东西并没有什么错,但它是任意的和个人的,因此可能应该被记录下来。自从我开始使用打字拍子以来,我一直给structs一个大写的首字母,我对此非常满意。类型化Racket中的大多数类型名称都以大写字母开头,而struct是一个类型名称,因此首字母大写的约定使struct名称看起来与其他类型一致
即使在现在的非类型化Racket中,我也一直遵循这个约定,当我将代码移动到类型化Racket时,它使代码清晰可读,没有问题