What's known about UTYPE! in REBOL 3?

梦想的初衷 提交于 2019-12-07 08:20:35

问题


The only information I can find on the datatype UTYPE! is "not yet been documented for R3" and "user defined datatype", still giving a shred of hope that I can break out of Rebol's canon of predefined datatypes and formulate the polymorphism of my functions in a more straightforward manner. Just... I've no idea how to deal with UTYPE!. Trying:

make utype! <2nd-arg>

with several kinds of arguments (including an object) was invariably leading to "Script error: invalid argument: <2nd-arg>".

So, how to operate with it? Is this feature implemented at all? And if not, is there anything known about how it is intended to work?

BTW, I'm well aware home brewed datatypes can be simulated by constructs like:

make object! [
    class: ...
    value: ...
]

Supplement, written on November 8:

Playing with UTYPE! effects HELP:

>> foo!: make utype! [[] [random: func [value] [42]]]
>> type? foo!
== utype!

>> ? echo
USAGE:
    ECHO target

DESCRIPTION:
    Copies console output to a file.
    ECHO is a native value.

ARGUMENTS:
REBOL System Error:
REBOL System Error #1224: assertion failed

Program terminated abnormally.
This should never happen.
Please contact www.REBOL.com with details.

(2.101.0.2.5 on Lion). There was certainly something going on under the hood.


回答1:


If you view the Rebol source code on github (https://github.com/rebol/rebol/blob/25033f897b2bd466068d7663563cd3ff64740b94/src/core/t-utype.c) it is clear that this feature has not been built yet.

Comment from the header of the file:

**  Notes:   NOT IMPLEMENTED

Searching through curecode.org brings back a number of comments which show the direction that is planned for the utype! datatype.

There's a plan to add user-defined datatypes - we even have a built-in type reserved for this, utype!. This would allow us to add new datatypes which respond to the actions, which would allow us to even have them support math operations if necessary. The only thing you wouldn't get is custom (non-construction) syntax for the type, or the ability to fit in a value slot. - BrianH

Source: http://curecode.org/rebol3/ticket.rsp?id=2137




回答2:


You can try an experimental implementation here

Utypes are implemented as objects, containing methods for actions and some natives. These methods are grouped in sub-object .methods

fraction!: make utype! [
  num: 0 den: 1
  .methods: object [
    .multiply: func [a b] [
      make fraction! [
        num: a/num * b/num
        den: a/den * b/den
      ]
    ]
    .add: ....
  ]
] 

You can control rendering via .form and .mold and comparison via .compare. Also math funcs are supported.

A complete example is complex module with relative demo

Any comment or suggestion is welcome!



来源:https://stackoverflow.com/questions/26780398/whats-known-about-utype-in-rebol-3

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!