Reputation: 18591
Seeing as you can limit the parameters of functions to specific datatypes, it stands to reason that you might want to define your own datatype, yet I cannot see anything in the Rebol docs that suggests this as a feature of the language (unless I didn't look very well).
What I am expecting is the ability to do something like the following:
mytype!: make datatype! ... ; some spec here
Is this possible? The following does not fill me with much hope:
http://www.rebol.it/giesse/custom-types.r
From the link:
Purpose: { Allows the programmer to define custom REBOL datatypes }
It is a rather lengthy piece of code. Not what I was hoping for.
Upvotes: 8
Views: 478
Reputation: 33607
Often suggested, as of today not implemented available only as an experimental patch by Giulio.
Any useful custom datatype proposals usually come along with the desire to hook them in so they could effectively "overload" things like + or append. There is an internal layer of abstraction called an ACTION! that in theory provides a place to put these hooks:
>> type? :append
== action!
Actions are a sort of "method call" (i.e. polymorphic) on the first argument, to which the ensuing parameters are passed. There is currently no exposed way for users to create actions or create a new datatype that responds to them.
For Rebol 3, user-defined datatypes are proposed under the moniker "utype" -- have a look at "What's known about UTYPE! in Rebol?" for more.
In the objects announcement for Red, however, I noticed some fine print at the end:
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.
Upvotes: 4
Reputation: 766
Tried an implementation of utypes in https://github.com/giuliolunati/rebol/tree/utype
As an example, I implemented complex! utype
Basically, utypes are implemented as objects with special methods in dotted form: so, .add implements the + op etc.
For now you can overload all actions (but make), and some natives (math functions, comparison, form, mold, print, probe)
Upvotes: 5