Re: routine_id help

new topic     » goto parent     » topic index » view thread      » older message » newer message

Roderick Jackson <rjackson at CSIWEB.COM> wrote:

>Okay, I'm stuck again and I need to once more draw from the infinite
Euphoria knowledge of the list. blink
>
>Anytime I begin to think I understand "routine_id", it throws me a new
curveball. I'm writing a library of routines that use "routine_id". One
global routine in the library attempts to get the ID of any arbitrary
user-defined function given to it. I've included the library file at the
very top of my test program, then below the include I have my arbitrary
functions, and finally my main procedure. For example:
>
>    ------------------------
>    -- mock library...
>    ------------------------
>   global function One ()
>   end function
>
>    -- more functions...
>
>   global function N (sequence FunctionName)
>      integer k
>       -- next line gets -1, even though the created function name is valid
>      k = routine_id (FunctionName & "_" & FunctionName)
>       -- and now it breaks...
>      return call_func (k, {})
>   end function
>    ------------------
>    -- end library
>    ------------------
>
>    ---------------------
>    -- test program
>    ---------------------
>   include library.e
>
>   global function MyFunction_MyFunction ()
>       -- process data...
>      return {}
>   end function
>
>    -- more functions...
>
>   procedure Main ()
>      object ReturnValue
>       -- process data...
>       -- next line crashes
>      ReturnValue = N ("MyFunction")
>      ? ReturnValue
>
>   end procedure
>
>   Main ()
>   -------------------------
>   end test program
>   -------------------------
>
>Now in my main procedure, I call the global library routine, and let it
construct (correctly) the name of one of my non-library functions. But using
"routine_id" to identify that name returns -1. I know the function being
identified must be visible; yet both the function using "routine_id" and the
one being identified are global. Am I still doing something illegal? Must
the library include the user-defined functions in order to access them?


The problem here stems from the "fine print" regarding how routine_id()
works. Here's the relevant sentence from LIBRARY.DOC, with emphasis by me:

"The routine named by s must be visible, i.e. CALLABLE, at the place where
routine_id is called to get the id number."

So, now you can see why this doesn't work -- routine_id() is being called
from N(), and MyFunction_MyFunction() is not CALLABLE from N(), so it's a
no-go.

To help clarify: *if* MyFunction_MyFunction() were defined *before* N(),
then it would work -- MyFunction_MyFunction() would be CALLABLE from N(),
and there would be no problem. But that's not the case.

Now, this kind of behavior seems stupid at first, but it allows you to do
some interesting things:


function old_open_id()
   return routine_id("open")
end function

function open(sequence file, sequence parms)
   ...
end function

function new_open_id()
   return routine_id("open")
end function


The way routine_id() currently works allows you to access both the original
*and* the redefined versions of the function. If routine_id() didn't work
this way, you could *only* receive the redefined function id, whether you
called old_open_id() or new_open_id().

But, while routine_id's behavior may be advantageous in *this* example, I'm
afraid that it goes very much *against* what you're trying to do with your
library, Rod. I, too, was frustrated when I discovered that I couldn't
"wrap" routine_id() in another function to enhance it, but at least the way
it currently works makes sense.

Hope this clears things up for you.

Gabriel Boehme

new topic     » goto parent     » topic index » view thread      » older message » newer message

Search



Quick Links

User menu

Not signed in.

Misc Menu