new 和 instantiate 之间的球拍区别

Racket difference between new and instantiate

table-panel 我偶然发现了对 instantiate 的调用。之前,在阅读 Racket 中 GUI 的文档时,我只看到 new 用于创建 GUI 类.

的对象

该页面中 instantiate 的用法:

(instantiate button%
        ((format "~a" j) child)
        (stretchable-width #t)
        (stretchable-height #t)
        (callback
         (lambda (button event)
           (printf "~a~n" (send button get-label)))))

new 在其余 documentation 中的用法:

; Make a frame by instantiating the frame% class
(define frame (new frame% [label "Example"]))

两者有什么区别?

编辑

我找到一个 documentation 页面告诉我区别,但我不太明白 "by-name initialization arguments" 是什么。这与关键字参数相同吗?

(define frame (new frame% [label "Example"]))中,[label "Example"]是一个别名初始化参数,名为label的参数被赋予值"Example"。它们在概念上类似于关键字参数,但机制不同,并且与关键字参数不同,如果您确实需要,它们可以按位置提供。这是相关文档,来自 https://docs.racket-lang.org/reference/createclass.html:

Initialization arguments can be provided by name or by position. The external name of an initialization variable can be used with instantiate or with the superclass initialization form. Those forms also accept by-position arguments. The make-object procedure and the superclass initialization procedure accept only by-position arguments.

Arguments provided by position are converted into by-name arguments using the order of init and init-field clauses and the order of variables within each clause. When an instantiate form provides both by-position and by-name arguments, the converted arguments are placed before by-name arguments. (The order can be significant; see also Creating Objects.)