[REBOL] returning an object from a function, sort of Re:
From: rebol:techscribe at: 17-Aug-2000 10:25
Hi Pete,
To be able to pass a word to your function and have the word set to the
object created in the function requires two changes to your code:
1. The argument must be declared using a lit-word! notation:
logo-factory: func [ 'name ] [ .... ]
note the tick in front of name, i.e. 'name
2. You must use the set function to assign the created object to the word
passed to the function, i.e.
set :name make object! [ ....]
The complete modified code follows:
logo-factory: func [
"Creates and returns a named logo object."
'name "The name of the object to be created."
][
set :name make object! [
type: "text"
text: ""
pict: #{}
file: ""
altt: ""
switch-type: func [
"Toggles the logo object's type between 'text' and 'pict'."
][
type: either type = "text" ["pict"]["text"]
]
]
]
>> logo-factory Logo1
>> print mold Logo1
make object! [
type: "text"
text: ""
pict: #{}
file: ""
altt: ""
switch-type: func [
{Toggles the logo object's type between 'text' and 'pict'.}
][
type: either type = "text" ["pict"] ["text"]
]
]
At 11:36 AM 8/17/00 -0400, you wrote:
>hiall
>
>btw, I sent a message almost identical to this one from my home account last
>night, and it isn't here. Is it because the list doesn't rec that other
account?
>anyway...
>
>I've got a function called logo-factory that accepts a string argument, and
>should return an object referenced by a word = the passed string. ie., if
I type
> *logo-factory Logo1*
>
>I should get the same result as if I typed
>
> *Logo1: make object! [....]*
>
>I'm befuddled as to how to do this exactly -- I'm sure the answer is right
under
>my nose ;-)
>
>Here's sort of what I have so far:
>
>*
>logo-factory: func [
> "Creates and returns a named logo object."
> name [string!] "The name of the object to be created."
>][
> ????? make object! [
> type: "text"
> text: ""
> pict: #{}
> file: ""
> altt: ""
> switch-type: func [
> "Toggles the logo object's type between 'text' and 'pict'."
> ][
> type: either type = "text" ["pict"]["text"]
> ]
> ]
>]
>*
>
>Obviously, there's a lot missing here, like handling an argument string which
>can't be used as a valid word, etc. Other functions like get-pict and
>write-html, etc., also need to be added.
>
>Any and all suggestions are welcome!
>
>--
>Pete Wason|"LWATPLOTG"|[zoon--stumpworld--c]|[mrzoon--hynoom--c]|CUCUG|TA|PHX
>
;- Elan [ : - ) ]
author of REBOL: THE OFFICIAL GUIDE
REBOL Press: The Official Source for REBOL Books
http://www.REBOLpress.com
visit me at http://www.TechScribe.com