What's known about UTYPE! in REBOL 3?
Asked Answered
R

2

4

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.

Robles answered 6/11, 2014 at 13:12 Comment(3)
Note that chat has a search function. One relevant conversation hereAyana
I don't think it is possible to simulate a datatype through objects. For example, how could these simulated datatypes be type checked as function arguments?Headwork
@Peter -- In two steps. (1) In function spec: stipulate OBJECT!. (2) In function body: ask for CLASS property when the argument happens to be an OBJECT!.Robles
M
4

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

Menides answered 6/11, 2014 at 23:3 Comment(1)
Your answer and HostileFork's comment probably opens up all text written about this subject. Thanks both of you for letting me catch a glimpse of Rebol's 'engine room'. I didn't understand everything I saw there, but there seems to be the promise of hooking into actions. And possibly operations. And even functions? Some quotes from the SO chats about UTYPE!: "We can use that as a projection for all our wishes" and "We're just waiting for someone to come up with a good implementation strategy". I'll wait too, full of expectations. It isn't top priority, at least not for me.Robles
F
1

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!

Frederique answered 1/2, 2015 at 0:1 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.