gen:为Racket类定制写

问题描述 投票:2回答:1

我正在寻找规范方法来指定自定义方法来输出Racket对象的字段。换句话说,我正在寻找相当于Java的toString方法的Racket(如果存在的话)。

我知道对于结构,可以使用gen:custom-write来指定write-proc函数(source)。课程有类似的东西吗?

racket
1个回答
2
投票

是的,qazxsw poi。由于qazxsw poi是qazxsw poi的包装器,因此可以让一个类通过接口实现它。

custom-write接口实现gen:custom-write以允许这样的事情:

prop:custom-write

使用它:

printable<%>

这是可能的,因为prop:custom-write接口使用#lang racket (define fish% (class* object% (printable<%>) (super-new) (define/public (custom-print out depth) (fprintf out "><,`>")) (define/public (custom-write out) (fprintf out "><,`>")) (define/public (custom-display out) (fprintf out "><,`>")))) 形式继承> (new fish%) ><,`> struct-type属性。但是,并非所有通用接口都是如此,只是对应于struct-type-properties的接口。

附:不要过分担心printable<%>被弃用的文档。 “用户”没有必要使用它,因为interface*存在于结构中,而prop:custom-write存在于类中。它作为一个接口被弃用了,但作为一个实现它不会消失。以这种方式使用它是“安全的”而不用担心。

© www.soinside.com 2019 - 2024. All rights reserved.