- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
在 python 对象中,覆盖方法 __repr__
和 __str__
一个对象的定义允许人们分别提供该对象的“明确的”和“人类可读的”表示。如何在 Racket 中实现类似的行为?
我遇到了 printable<%>
界面 here ,看起来它应该可用于此目的,但我无法让它像我期望的那样工作。基于文档中的标准“鱼”示例:
(define fish%
(class* object% (printable<%>)
(init size) ; initialization argument
(super-new) ; superclass initialization
;; Field
(define current-size size)
;; Public methods
(define/public (get-size)
current-size)
(define/public (grow amt)
(set! current-size (+ amt current-size)))
(define/public (eat other-fish)
(grow (send other-fish get-size)))
;; implement printable interface
(define/public (custom-print port quoting-depth)
(print "Print called!"))
(define/public (custom-write port)
(print "Write called!"))
(define/public (custom-display port)
(print "Display called!"))))
这是我得到的输出:
> (define f (new fish% [size 10]))
> f
"Display called!""Display called!""Print called!"
> (print f)
"Write called!""Print called!"
> (display f)
"Display called!""Display called!"
> (write f)
"Write called!""Write called!"
>
所以这个问题有三个方面:
为什么它会以这种方式运行,即在对象的明显单一渲染上调用多个方法?
custom-print、custom-write 和 custom-display 方法的计算结果应该是什么?他们应该简单地返回一个字符串,还是应该实际承担打印、写入或显示的副作用(视情况而定)?例如。自定义写入方法是否应调用 write
内部功能?
这是用于此目的的正确结构吗?如果没有,有没有/它是什么?
最佳答案
至于
- Why does it behave this way, i.e. with the multiple methods being invoked on an apparently singular rendering of the object?
你不小心在write
中使用了print
,所以写入值时,会先打印值。
(define/public (custom-write port)
(print "Write called!"))
display
中也存在类似的问题。
还要记住打印/写入/显示到正确的端口。
尝试
#lang racket
(define fish%
(class* object% (printable<%>)
(init size) ; initialization argument
(super-new) ; superclass initialization
;; Field
(define current-size size)
;; Public methods
(define/public (get-size)
current-size)
(define/public (grow amt)
(set! current-size (+ amt current-size)))
(define/public (eat other-fish)
(grow (send other-fish get-size)))
;; implement printable interface
(define/public (custom-print port quoting-depth)
(print (~a "Print " current-size "\n") port))
(define/public (custom-write port)
(write (~a "Write " current-size "\n") port))
(define/public (custom-display port)
(display (~a "Display " current-size "\n") port))))
在repl中你会看到:
> (define f (new fish% [size 10]))
> f
"Print 10\n"
> (display f)
Display 10
> (write f)
"Write 10\n"
关于oop - python 的 __repr__ 和 __str__ 的 Racket 等价物?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55975708/
我意识到“强类型”和“软类型”的定义是松散且易于解释的,但我还没有找到关于无类型 Racket (根据我的理解意味着动态类型)和类型 Racket 的明确定义。 再一次,我确定它不是那么简单和干燥,但
我来自 Java 和 Python,很难理解面向对象代码在 Racket 中的工作方式。 给定 (define food% (class object% (super-new) (i
我安装了 Sublime Text - Racket Package,但没有用。出现此错误。我该怎么办。 [Errno 2] No such file or directory: 'racke
.rkt是“常规”Racket 源代码的常规文件扩展名。在文档中,我还看到了 .rktl 和 .rkts 正在使用。什么是.rktl和 .rkts用于,还有我不知道的更多 Racket 文件扩展名吗?
假设我为具有“中心”的集合定义了一个结构体。 (struct centered-set (center elems)) 我想保证以下条件。 elems是一个集合。 center是 elems 的成员(
我想要一个函数,它接受一个字符串,由一个 python 格式的树组成,就像这样 "[0, [1, 0]]" 并输出一个可用的 Racket 列表,像这样 '(0 (1 0)) 我的第一步是将字符串转换
是否有 Racket 内置函数列表?我正在寻找类似 this one 的列表对于 python 。我似乎无法在文档中找到一个。 最佳答案 这里有一些列表: Racket文档中所有函数的索引: http
虽然 Racket 允许创建 GUI 应用程序(甚至游戏),但我想知道它是否适合与 Things 一样在视觉上发布商业应用程序或 OmniOutliner .这样的应用程序会感觉原生还是看起来像一个不
和node的npm一样,我们使用package.json来解决依赖。 我试过raco pkg,它似乎只是一种开发/发布包的方式,而不是解决外部依赖。 最佳答案 如上所述,Racket 中的 info.
我是 Racket 的新手,尽管到目前为止我已经设法使用 servlet 并让一些东西发挥作用。我现在想做的是验证一个简单的用户名/密码 POST 请求。我已经通过“web-server/formle
我正在为现场编码表演创建一个程序,为此我需要一个基本的 S 表达式代码编辑器(我输入的内容将在适当的语法上下文中评估为 Racket 代码)。 由于 DrRacket 本身是用 Racket 编写的,
和node的npm一样,我们使用package.json来解决依赖。 我试过raco pkg,它似乎只是一种开发/发布包的方式,而不是解决外部依赖。 最佳答案 如上所述,Racket 中的 info.
我正在尝试在 Racket 中编写模块元语言 mylang ,它接受将修改后的主体传递给的第二种语言,例如: (module foo mylang typed/racket body) 相当于: (m
我没有运气找到一种方法来按照我想要的方式进行这项工作,所以如果有人能提供帮助,我将不胜感激。 我想要的是能够在终端上做到这一点: > racket 然后能够输入 Racket 命令并让它们编译。像,
我来自 Java 世界,对 Racket 充满热情。 想知道类似Maven的工具在哪里。如果你不熟悉Maven,还有Scala中的SBT,Android中的gradle,Elixir中的mix等,作为
在 typed/racket 我有一个像 [(? procedure? p ) (apply p xv*)]会导致错误: Type Checker: Function has no cases in:
我是 Lisp-scheme 的新手,对整个函数范式还很陌生,我目前正在做一个任务,要求我重载一个名称相同但参数集不同的函数 racket .下面是我试图实现的一个例子: #lang racket (
有没有办法在 Racket 中获取对象的唯一标识符?例如,当我们使用 Racket 的 eq?运算符来检查两个变量是否指向同一个对象,它使用什么标识符来实现这种比较? 我正在寻找类似 python 的
当我在 Dr. Racket 或从命令行运行我的测试时,失败的测试总是在一行上。 例如, (bad (interp-t-prog (list '(class posn extends object (
我有以下代码: (plot (function sin (- pi) pi #:label "y = sin(x)")) 但是在解决 SICP 时我没有遇到这样的结构:#:label "y = sin
我是一名优秀的程序员,十分优秀!