Racket 尝试在trait中的重写方法之外调用'super',该trait有两个'quot;“相互递归”;方法,但失败
最近我在学习球拍的特质系统 我用两种相互递归的方法做了一些关于Racket 尝试在trait中的重写方法之外调用'super',该trait有两个'quot;“相互递归”;方法,但失败,racket,traits,mixins,Racket,Traits,Mixins,最近我在学习球拍的特质系统 我用两种相互递归的方法做了一些关于super调用的实验: 我首先尝试了mixin,它的工作原理与预期一致: #lang racket (define fish% (class object% (super-new) (define/public (get-color) (println "fish% get-color")) (define/public (get-price) (println &qu
super
调用的实验:
我首先尝试了mixin,它的工作原理与预期一致:
#lang racket
(define fish%
(class object% (super-new)
(define/public (get-color)
(println "fish% get-color"))
(define/public (get-price)
(println "fish% get-price"))
))
(define (spots-mixin %)
(class % (super-new)
(define/override (get-color)
(println "spots-mixin get-color")
(super get-price))
(define/override (get-price)
(println "spots-mixin get-price")
(super get-color)) ))
(define spots%
(spots-mixin fish%))
(send (new spots%) get-color)
(send (new spots%) get-price)
; Output:
; "spots-mixin get-color"
; "fish% get-price"
; "spots-mixin get-price"
; "fish% get-color"
然后我尝试了这种特质,但失败了:
#lang racket
(require racket/trait)
(define fish%
(class object% (super-new)
(define/public (get-color)
(println "fish% get-color"))
(define/public (get-price)
(println "fish% get-price"))
))
(define spots-trait
(trait
(define/override (get-color)
(println "spots-trait get-color")
(super get-price))
(define/override (get-price)
(println "spots-trait get-price")
(super get-color))
))
(define spots%
((trait->mixin spots-trait) fish%))
(send (new spots%) get-color)
(send (new spots%) get-price)
答复者告诉我:
; class*: superclass does not provide an expected method for override
; override name: member68863
; class name: ...cts/racket/trait.rkt:367:17
我只在超类fish%
中定义了两个方法,在traitspots trait
中定义了两个方法来覆盖它们
为什么超类不提供预期的重写方法
有没有办法修复此错误
顺便说一句,如果我在spots trait
中声明一个额外的(继承/super-get-color-get-price)
,REPL会抱怨:
get-color: duplicate definition of external name in trait
; in: get-color
非常感谢
更新:
最近我重读了一篇论文“类、混合和特征的方案”,其中提到
如果在重写实现以外的方法中允许超级调用,则需要特别小心,并且相互超级调用方法的循环可能需要间接寻址,以防止超级调用访问trait中而不是基类中的实现。幸运的是,trait应用程序操作符可以自动生成此间接寻址
这似乎与这个问题有关,但我不太明白它的意思。(例如,什么是“间接”)