Mailing List Archive: 49091 messages
  • Home
  • Script library
  • AltME Archive
  • Mailing list
  • Articles Index
  • Site search
 

Troube Calling Function within Object

 [1/6] from: cribbsj::oakwood::org at: 28-Nov-2000 14:02


I hope someone can help me with this one 'cause I have been wrestling with it for two days and I'm about to tear out what little hair I have left! Let's say I have an object called player. Within that object I have created a function called find-records. find-records expects a block to be passed to it as an argument. My problem is this: I want to call this function using the value of a variable called table and return the value to a higher calling function. So far, I have gotten this to work: return player/find-records next request (request is the block I want to pass, excluding the first value). Now, let's say I do this: table: "player" How do I call the function now? I have tried about a million different variations of: return to-word to-path reduce [table 'find-records] next request (I use return here because I am calling this line from a higher level function and I want to return the value). But nothing seems to work! Sometimes, it appears I was able to actually call the function within the object, but then the argument won't get passed. Sometimes, I think I got the function to be called WITH the arguments, but I can't get the return value from the function to be returned by this line. I have read and re-read the parts in the manual dealing with words, functions, evalution, etc. and I just can't find what I am doing wrong. I can email my script to anyone who needs to see it. I appreciate any help that can be given. Jamey Cribbs

 [2/6] from: jelinem1:nationwide at: 28-Nov-2000 14:14


I followed your thought right up until: table: "player" Now, a whole bunch of scope-related questions pop up, and I don't see how to give an answer without looking at the full script. Probably all you have to do is convert the string "player" to a word, then bind it to the context which you've defined 'player as the object. Michael Jelinek work: [jelinem1--nationwide--com] home: [michael1356--home--com] everquest: [matsumurak--home--com] Jamey Cribbs <[cribbsj--oakwood--org]>@rebol.com on 11/28/2000 01:02:49 PM From: Jamey Cribbs <[cribbsj--oakwood--org]>@rebol.com on 11/28/2000 01:02 PM Please respond to [rebol-list--rebol--com] Sent by: [rebol-bounce--rebol--com] To: "Rebol List" <[rebol-list--rebol--com]> cc: Subject: [REBOL] Troube Calling Function within Object I hope someone can help me with this one 'cause I have been wrestling with it for two days and I'm about to tear out what little hair I have left! Let's say I have an object called player. Within that object I have created a function called find-records. find-records expects a block to be passed to it as an argument. My problem is this: I want to call this function using the value of a variable called table and return the value to a higher calling function. So far, I have gotten this to work: return player/find-records next request (request is the block I want to pass, excluding the first value). Now, let's say I do this: table: "player" How do I call the function now? I have tried about a million different variations of: return to-word to-path reduce [table 'find-records] next request (I use return here because I am calling this line from a higher level function and I want to return the value). But nothing seems to work! Sometimes, it appears I was able to actually call the function within the object, but then the argument won't get passed. Sometimes, I think I got the function to be called WITH the arguments, but I can't get the return value from the function to be returned by this line. I have read and re-read the parts in the manual dealing with words, functions, evalution, etc. and I just can't find what I am doing wrong. I can email my script to anyone who needs to see it. I appreciate any help that can be given. Jamey Cribbs

 [3/6] from: cribbsj:oakwood at: 28-Nov-2000 15:36


Michael, I get the concept of converting the string "player" to a word, but how do I bind it to the actual player object? Thanks! Jamey. On Tuesday 28 November 2000 15:14, you wrote:

 [4/6] from: larry:ecotope at: 28-Nov-2000 12:58


Hi Jamey You could try something along these lines.
>> player: make object! [f: func [x][x * x]] >> table: "player"
== "player"
>> do reduce [get in get to-word table 'f 6]
== 36 When the block is reduced TABLE evaluates to its string value "player", GET returns the object named player, IN returns a reference to the field named f, the outer GET returns the function f itself. This is the first element of the reduced block, the second is the reduced value of 6 which is still 6. Finally, the do applies the function to the argument 6. Using this approach will cause the argument of the function to be evaluated twice, once by reduce and again when the do applies the function to the argument. Or, more simply, you can also just create a word in the outer context to hold the function and use it normally.
>> ff: get in get to-word table 'f >> ff 5
== 25 HTH -Larry

 [5/6] from: cribbsj:oakwood at: 28-Nov-2000 16:52


Larry, you are a genuis! It worked! Thanks for the help! Jamey. On Tuesday 28 November 2000 15:58, you wrote:

 [6/6] from: jelinem1:nationwide at: 28-Nov-2000 15:43


USAGE: BIND words known-word /copy DESCRIPTION: Binds words to a specified context. BIND is a native value. ARGUMENTS: words -- A block of words or single word. (Type: block word) known-word -- A sample word from the target context. (Type: word) REFINEMENTS: /copy -- Deep copies block before binding it. known-word must be a word which was defined within the same scope as when you defined the 'player word. The problem then becomes one of finding this word. For example: ; --------- START CODE ----------- player: make object! [ ; Really this only sizes the block, ignoring the first element find-records: func [arg [block!]][return length? arg] ] known-word: none some-func: func [table [string!] request [block!]][ bind some-word: to-word table 'known-word return do get (in get some-word 'find-records) next request ] print some-func "player" ["garbage" "b" "c" "d"] ;-------- END CODE --------------- Good thing I tested this - not quite as straight forward as I thought, but do-able. As long as you define 'player in the global context, it should be easy to find another word to 'bind with. - Michael Jelinek Jamey Cribbs <[cribbsj--oakwood--org]>@rebol.com on 11/28/2000 02:36:57 PM From: Jamey Cribbs <[cribbsj--oakwood--org]>@rebol.com on 11/28/2000 02:36 PM Please respond to [rebol-list--rebol--com] Sent by: [rebol-bounce--rebol--com] To: [rebol-list--rebol--com] cc: Subject: [REBOL] Re: Troube Calling Function within Object Michael, I get the concept of converting the string "player" to a word, but how do I bind it to the actual player object? Thanks! Jamey. On Tuesday 28 November 2000 15:14, you wrote: