Return 仅对象词,不包括以冒号为后缀定义的函数

Return only object words but not functions defined with colon as the suffix

创建 object! 时如下所示:

REBOL []


Room: make object! [
    price: copy ""
    area: copy ""

    total: func [] [
        price * 2
    ]

    set 'total2 func [] [
        price * 3
    ]
]

probe Room的结果是:

make object! [
    price: ""
    area: ""
    total: make function! [[][
        price * 2
    ]]
]

打印了函数total:,但是没有打印函数total2。我有一个脚本,我将 map! 保存为一系列 repend [hash Room]。在我添加函数 total 之前,Room 对象将 return 只有单词,例如

make object! [
    price: ""
    area: ""
] 

添加 total 函数后,它也被 returned 修改了我想避免的输出。那么,除了将 total: 更改为 set 'total 之外,有没有办法只 return 先前的输出?

Rebol/Saphir book里面有一个/display字或者细化,但是在我的Atronix 3.0.0.4.40系统里好像不行:

** Script error: cannot access display in path Room/display

那怎么办?创建 pricearea 及其值的 map!?

The function total: is printed, but the function total2 is not.

在绑定方面,您的 total2 绑定到封闭上下文...而不是对象。因此,如果您要从控制台 运行:

>> Room: make object! [
    price: copy ""
    area: copy ""

    total: func [] [
        price * 2
    ]

    set 'total2 func [] [
        price * 3
    ]
]

>> probe :total2
make function! [[][price * 3]]

听起来您首先这样做是因为您想要 "hide" 对象输出中的函数,但仍然保留它们。通常,如果您不想看到这些函数,那么探测或调试转储一个对象作为序列化它的方式可能不是最好的主意。

关于 Rebol 中对象工作方式的技术要点:当您像这样将函数成员值放入对象中时,它们实际上是 unique 函数。因此,每个具有函数成员的对象都有一个主体副本,其单词专门绑定到 该对象实例 的单词标识。虽然有点性能难题,但让每个对象完全独立可以自定义每个对象。但是,如果这对您的目的来说是个问题,那么您可以将函数保留在对象之外。

如果这不是问题,您可能应该为这些情况编写一个序列化函数,因为这些函数只是 in 对象正在困扰您。

虽然在这种情况下这不是您想要的,但我会提到一些很酷的东西... PROTECT/HIDE:

>> Room: make object! [
    price: '000'000.99
    area: 20x40

    total: func [] [
        price * 2
    ]

    sneaky: does [total]
]

>> protect/hide 'room/total

>> room
== make object! [
    price: 1000000.99
    area: 20x40
    sneaky: make function! [[][total]]
]

>> room/total
** Script error: cannot access total in path room/total

>> room/sneaky
== 2000001.98

这是一种从可见性或绑定中删除内容的方法,但任何以前存在的绑定仍然有效。如果您将它用作 MAKE! 的父对象,隐藏的项目仍然存在:

>> o: make Room [price: 4 area: 10x20]
== make object! [
    price: 4
    area: 10x20
    sneaky: make function! [[][total]]
]

>> o/sneaky
== 8

>> o/total
** Script error: cannot access total in path o/total

>> o/total: does [print "can't overwrite it, either!"]
** Script error: cannot access total in path o/total: