returning an object from a function, sort of
[1/5] from: zoon::stumpworld::com at: 17-Aug-2000 11:36
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
[2/5] from: jelinem1:nationwide at: 17-Aug-2000 11:07
You were close. Drop the 'name argument and use the following syntax to
create your object:
Logo1: logo-factory
I suppose you COULD keep the 'name argument, then use 'set within the
function to give this a value, but I use the example above to create my
objects. There are a couple more details to the argument approach; if you
really want to do things this way just ask and I'll spell it out.
- Michael Jelinek
[zoon--stumpworld--com] on 08/17/2000 10:36:41 AM
From: [zoon--stumpworld--com] on 08/17/2000 10:36 AM
Please respond to [list--rebol--com]
To: [list--rebol--com]
cc:
Subject: [REBOL] returning an object from a function, sort of
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
[3/5] from: larry:ecotope at: 17-Aug-2000 10:10
Hi Pete
Not sure exactly what you want, but here is a start. Because you want the
arg of the factory function to become the name of an object, it is probably
best to require it to be a word. The interpreter will then throw an error
if the arg is not a valid REBOL word before the function is called. This is
also consistent with your stated call syntax
>> logo-factory Logo1 ; the arg Logo1 is a word in this call, not a
string
We don't want the arg to be evaluated, so we need to quote it in the arg
list (there are other ways of accomplishing the goal, but this is a good
one). The following function generates the named object and also shows how
to use the given arg in definitions local to the returned object.
>> logo-factory: func ['arg [word!]] [set arg make object! [name: arg]]
>> logo-factory logo1 ;note REBOL is case insensitive
>> probe logo1
make object! [
name: 'logo1
]
>> logo1/name
== logo1
>> type? logo1/name
== word!
HTH
-Larry
[4/5] 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:
<<quoted lines omitted: 23>>
>--
>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
[5/5] from: zoon:stumpworld at: 17-Aug-2000 16:53
[rebol--techscribe--com] wrote:
> 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:
I could also do it like:
logo-factory: func [
[ "Creates and returns a named logo object."
[ 'name [word!] "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"]
[ ]
[ ]
[ ]
example use:
>> logo-factory Logo1
>> logo-factory Logo2
>> probe 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"]
]
]
>> Logo1/text: "Emporium of Liquid Fun"
== "Emporium of Liquid Fun"
>> Logo1/switch-type
== "pict"
>> probe Logo1
make object! [
type: "pict"
text: "Emporium of Liquid Fun"
pict: #{}
file: ""
altt: ""
switch-type: func [
{Toggles the logo object's type between 'text' and 'pict'.}
][
type: either type = "text" ["pict"] ["text"]
]
]
>> probe Logo2
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"]
]
]
But, of course, after all that, I see now that it might be better if the object
were internally identified, rather than depending upon the word that happens to
reference it ;-)
make object! [
name: "Logo1"
type: "Primary Store Logo"
styl: "text"
text: "Emporium of Liquid Fun"
etc...
--
Pete Wason|"LWATPLOTG"|[zoon--stumpworld--c]|[mrzoon--hynoom--c]|CUCUG|TA|PHX
Notes
- Quoted lines have been omitted from some messages.
View the message alone to see the lines that have been omitted