REBOL 3.0

Comments on: Changes in MAKE function!

Carl Sassenrath, CTO
REBOL Technologies
27-Jun-2007 16:15 GMT

Article #0093
Main page || Index || Prior Article [0092] || Next Article [0094] || 9 Comments || Send feedback

As you know, the datatype creator make accepts a datatype specifier or a prototype:

s: make string! 123
t: "abc"
s: make t 123

It is most useful in the case of objects:

ob1: make object! [a: 10]
ob2: make ob1 [b: 20]
print ob2
a: 10
b: 20

During R3 testing, this was listed as a bug:

f1: func [a] [print a]
f2: make :f1 []

That expression would read: make f2 using f1 as the prototype.

Definition

To make that work, it had to be defined. Here it is the definition...

First, recognize that in R3 the general constructor is:

f: make function [ [spec] [body] ]

Just use source on func to see such details.

So, now you know the format for make function prototype. It is the same.

f2: make :f1 []  ; just copy the function
f3: make :f1 [[body]] ; keep spec the same, use new body
f4: make :f1 [[spec][body]] ; replace spec and body

The f3 case is the most useful. It lets you "inherit" the function specification (e.g. argument list), but redefine the body of the function. F2 and f4 cases are just for completion of the form.

As a trivial example:

mo-string: make :to-string [[mold value]]

Also...

An additional note: you can also use copy now, instead of the f2 case above.

f5: copy :f1

Note that all parts are deep copied and rebound to the new function context.

9 Comments

Comments:

oldes
27-Jun-2007 18:22:13
I'm not sure how much it's useful to share the spec block because if you modify body, you should change description as well, which is in the shared spec.
Goldevil
27-Jun-2007 18:55:11
It can be useful for debugging : I like to change the behaviour of some function in another part of the script (replace by console output,force the return of a specific value,...)

Isn't it ?

-pekr-
28-Jun-2007 2:54
Carl,

not sure above is consistent. How is that first element of function spec block refers once to body, and for the second time to spec of the function? So, as for me, I might get used to it, but I have objection to how 'f3 is explained. If we want to have it consistent, it should look:

f3: make :f1 [ [] [body] ]

It is both consistent with 'f2 (you use empty block for the copy of supposed element, and, the order of blocks is still kept correct - first one is spec, second one is body.

I know it might not look so elegant as yours 'f3, but imo users might appreciate it, when dynamically inspecting function definition. In your example, we will have to check for the length of the block first, to just find out, if the first element is spec, or a body ...

Petr

Brian Hawley
28-Jun-2007 16:44:06
Petr, [] is a valid spec, so how would REBOL tell the difference between f3 an f4 in your notation?
Ingo
28-Jun-2007 18:11:20
I find pekrs point valid, so how about

f3: make :f1 [none [body]]

Like goldevil I have had uses of this pattern, though being able to keep the spec, but change the main description would be very handy.

-pekr-
29-Jun-2007 1:44:24
Brian, maybe I don't understand the issue properly, but what do you mean? Isn't difference between 'f3 and 'f4 clear?

f3: make :f1 [ [] [body] ]
f4: make :f1 [ [spec] [body] ]

rebolek
29-Jun-2007 2:37:23
Pekr, how would you do this using your example?

;I have -

f1: func [a][print "I take one parameter"]

;and I want -

f2: func [][print "I take no parameter"]

f2: make f1 [[][print "I take no parameter"]]

first f2

== [a]

Using your example, I'm not able to make a function with empty spec, which is valid REBOL function. But with Ingo's example it's possible, using [none [body]].

Jeff M
29-Jun-2007 13:55:46
What about the functions make them special from regular objects? Instead of the definition of a function being

f1: make function [ [spec] [body] ]

Let it be:

f1: make function [ 
    spec: [spec] 
    body: [body] 
]

Now it works just as one would expect, is more flexible (why not add documentation: and other fields?) and it's possible to add extra information into a function if desired:

f2: make :f1 [
    rev-2: "Fixed by Bob on 12/11/07" 
    body: [ new code here ]
]
Scot
5-Jul-2007 17:18:58
Functions are not objects and I really like the difference in REBOL. I'm not a fan of OOP. I do like the idea of creating new functions with a template made from the interface of the old function. I like this implementation.

Post a Comment:

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

Name:

Blog id:

R3-0093


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 20-Apr-2024 - Edit - Copyright REBOL Technologies - REBOL.net