Leaky Functions

Carl Sassenrath, CTO
REBOL Technologies
8-May-2006 23:46 GMT

Article #0025
Main page || Index || Prior Article [0024] || Next Article [0026] || 22 Comments || Send feedback

By default, all REBOL user-defined functions return the last value of their evaluation. For example, you can write:

add3: func [a b c] [a + b + c]

This is a shortcut for:

add3: func [a b c] [return a + b + c]

(And not using the return function is also faster.)

However, REBOL 3.0 will be adding modules, and modules have the ability to hide their words and internal data. This may be done for security reasons, but also for better programming abstraction enforcement. For example, you may use a module that processes passwords (that you don't want scripts to be able to see or access).

The problem is, if a programmer is not careful, the standard function return above can create leaky functions that may accidentally return important internal data.

Here's a simple illustration from part of a module:

passwords: []  ; private list of passwords

add-password: func [new-pass] [
    append passwords new-pass

The add-password function is exported, but the password list is not. Unfortunately, the programmer was not careful, and the add-password function ends up returning the entire password list as a result! Not good.

A simple solution is:

add-password: func [new-pass] [
    append passwords new-pass
    none  ; or, you could use exit too

but, some programmers will not remember (or know) to do that.

So, we have to ask, what's a good solution to this problem? Do we want to enforce a specific usage pattern (e.g. function blocks must always use a return or exit) or do we want to develop some kind of optional attribute (perhaps within the module specification) to help avoid this situation?

Of course, we cannot change the default function return case (as shown in the first example) because it would break far to much code. But, I think we do have other options, and I'm sure there are some I've not thought of yet. Let's hear some of your ideas.


Updated 14-Jun-2024 - Edit - Copyright REBOL Technologies -