REBOL 3.0

Comments on: Modules: Compatibility with REBOL V2

Carl Sassenrath, CTO
REBOL Technologies
6-Oct-2006 19:43 GMT

Article #0047
Main page || Index || Prior Article [0046] || Next Article [0048] || 2 Comments || Send feedback

There have been questions about how to create modules that are compatible between REBOL V2 and V3.

Generally, modules written for V2 will work in V3 (all other compatibility issues aside, such as PORTs). That is, if in V2 you load a file that provides a module to your program, and you call its functions, that will work the same way in V3.

The reason that this works is because such files are "unofficial modules" or "modules by convention". They are nothing more than additional code sections for your script... as if they appeared directly within your script.

REBOL V3 provides an official definition of modules. In a nutshell, modules give you control over contexts (think of them like you do objects). For files, the script header object determines what is officially a module in V3.

The default format of a module is trivial. (This is also done to provide compatibility, but also to encourage all programmers, even beginners to strongly consider writing their own modules, when it makes sense to do so.) Only one extra line in the header is needed to make any script file into a module:

module: 'mod-example

Here defining module triggers REBOL to process the file as a module, and mod-example provides the name of the module. The name gives you a way to identify the module later within the REBOL system. (For example, if you want to purge the module from the system.)

Here is a valid REBOL V3 module:

REBOL [
    title: "Stock Handler"
    module: 'mod-stocks
]

stocks: []

buy: func [symbol price] [
    repend stocks [symbol price]
]

sell: func [symbol price] [
    remove-all [asymbol aprice] stocks [
        symbol = asymbol
    ]
]

Note that by default, this module exports all of its defined values. The buy and sell functions are exported, and so is the stocks block. Again, this behavior was selected to make it easy for beginners to write their own modules.

Programmers who have a little more experience may choose to make some of the module values private and others public. Again, such requirements can be specified in the header. Here is the revised script that only exports the buy and sell functions:

REBOL [
    title: "Stock Handler"
    module: 'mod-stocks
    export: [buy sell]
]

stocks: []

buy: func [symbol price] [
    repend stocks [symbol price]
]

sell: func [symbol price] [
    remove-all [asymbol aprice] stocks [
        symbol = asymbol
    ]
]

The export specifies only buy and sell be exported. The stocks word is not exported, so its value cannot be accessed outside of the module.

2 Comments

Comments:

Scot
9-Oct-2006 22:12:21
That is very cool. I figured it wouldn't be hard to use R2 code, but not quite as easy as that. Very cool.
Peter Wood
10-Oct-2006 6:28:47
Does "Only one extra line in the header is needed to make any script file into a module:" imply that a module is restricted to a single file?

Post a Comment:

You can post a comment here. Keep it on-topic.

Name:

Blog id:

R3-0047


Comment:


 Note: HTML tags allowed for: b i u li ol ul font span div a p br pre tt blockquote
 
 

This is a technical blog related to the above topic. We reserve the right to remove comments that are off-topic, irrelevant links, advertisements, spams, personal attacks, politics, religion, etc.

REBOL 3.0
Updated 24-Apr-2024 - Edit - Copyright REBOL Technologies - REBOL.net