你如何在 Rebol 中定义你自己的数据类型?

How do you define your own datatype in Rebol?

既然您可以将函数的参数限制为特定的数据类型,那么您可能想要定义自己的数据类型是理所当然的,但我在 Rebol 文档中看不到任何暗示这是该语言特性的内容(除非我看起来不太好)

我期望的是能够执行以下操作:

mytype!: make datatype! ... ; some spec here

这可能吗?以下并没有让我抱太大希望:

http://www.rebol.it/giesse/custom-types.r

来自link:

Purpose: { Allows the programmer to define custom REBOL datatypes }

这是一段相当冗长的代码。不是我所希望的。

经常建议,截至今天未实施 .

任何有用的自定义数据类型提案通常都伴随着将它们挂钩的愿望,因此它们可以有效地 "overload" 诸如 +append。有一个称为 ACTION! 的内部抽象层,理论上它提供了放置这些钩子的地方:

>> type? :append
== action!

Actions 是第一个参数的一种 "method call"(即多态),随后的参数将传递给它。目前没有公开的方式供用户创建操作或创建响应它们的新数据类型。

对于 Rebol 3,在名称 "utype" 下提出了用户定义的数据类型——查看 "What's known about UTYPE! in Rebol?" 了解更多信息。

但是,在 objects announcement for Red 中,我注意到最后有一些精美的印刷品:

In order to help the Red compiler produce shorter and faster code, a new #alias compilation directive will be introduced. This directive will allow users to turn an object definition into a "virtual" type that can be used in type spec blocks. For example:

#alias book!: object [
    title: author: year: none
    banner: does [form reduce [author "wrote" title "in" year]]
]

display: func [b [book!]][
    print b/banner
]

This addition would not only permit finer-grained type checking for arguments, but also help the user better document their code.

尝试在 https://github.com/giuliolunati/rebol/tree/utype

中实现 utype

举个例子,我实现了complex! utype

基本上,utypes 被实现为具有点分形式的特殊方法的对象:因此,.add 实现了 + op 等。

现在您可以重载所有动作(但 make 除外)和一些原生动作(数学函数、比较、表格、模具、打印、探测)