1. routine_id help

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 =3D 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 =3D 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?


Rod Jackson

new topic     » topic index » view message » categorize

2. Re: routine_id help

Oh, it might help to know that I'm running version 2.0. Then again, it =
might not...

----------
From:   Roderick Jackson[SMTP:rjackson at csiweb.com]
Sent:   Tuesday, February 16, 1999 10:47 AM
To:     'Euphoria'
Subject:        routine_id help

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 =3D 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 =3D 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?


Rod Jackson

new topic     » goto parent     » topic index » view message » categorize

3. Re: routine_id help

On Tue, 16 Feb 1999, Roderick Jackson 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.

[Example snipped for space, see Rod's previous post for code]

As far as N() is concerned in your program, MyFunction_MyFunction() hasn't
been declared, because N() occurs before it. A proc/func in Euphoria can
only see what is before it in the code.

I suggest you move N() into a second .E library and include it after all
of your function definitions, like so:

-- library.e --------------------------------------

global function One()
    return 1
end function

-- other functions here...
-- N() has been moved to library2.e

-- end of library.e -------------------------------

-- library2.e -------------------------------------

function N()
    -- Rod's code here
end function

-- end if library2.e ------------------------------

-- program.ex -------------------------------------

include library.e -- for One() et al.

-- program.ex's local functions here

include library2.e -- for N()

-- No more proc/func definitions here!

procedure Main()
    -- etc.
end procedure

-- end of program.ex ------------------------------

HTH,
Carl

--
Carl R White -- Final Year Computer Science at the University of Bradford
E-mail........: cyrek- at -bigfoot.com -- Remove hyphens. Ta :)
URL...........: http://www.bigfoot.com/~cyrek/
Uncrackable...: "19.6A.23.38.52.73.45 25.31.1C 3C.53.44.39.58"

new topic     » goto parent     » topic index » view message » categorize

4. Re: routine_id help

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 message » categorize

5. Re: routine_id help

Hmmm, thanks for the advice (and thanks to Gabriel too.) This clears =
things up, but also hinders what I was doing.

After posting, I experimented a bit; it indeed appears that "routine_id" =
can't access ANY forward routines, even in the same file. This doesn't =
sound right... the documentation states that forward access can be =
achieved, and even notes that mutual recursion is possible using the =
function. I had been working under the assumption that visibility was =
only limited by global/local/private scope.

Just out of curiosity, if "routine_id" can't call a forward routine, how =
on earth can Euphoria implement mutual recursion? Perhaps my copy of =
refman.doc is out of date?


Rod Jackson

----------
From:   Carl R. White[SMTP:C.R.White at SCM.BRAD.AC.UK]
Sent:   Tuesday, February 16, 1999 11:22 AM
To:     EUPHORIA at LISTSERV.MUOHIO.EDU
Subject:        Re: routine_id help

On Tue, 16 Feb 1999, Roderick Jackson 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.

[Example snipped for space, see Rod's previous post for code]

As far as N() is concerned in your program, MyFunction_MyFunction() =
hasn't
been declared, because N() occurs before it. A proc/func in Euphoria can
only see what is before it in the code.

I suggest you move N() into a second .E library and include it after all
of your function definitions, like so:

-- library.e --------------------------------------

global function One()
    return 1
end function

-- other functions here...
-- N() has been moved to library2.e

-- end of library.e -------------------------------

-- library2.e -------------------------------------

function N()
    -- Rod's code here
end function

-- end if library2.e ------------------------------

-- program.ex -------------------------------------

include library.e -- for One() et al.

-- program.ex's local functions here

include library2.e -- for N()

-- No more proc/func definitions here!

procedure Main()
    -- etc.
end procedure

-- end of program.ex ------------------------------

HTH,
Carl

--
Carl R White -- Final Year Computer Science at the University of =
Bradford
E-mail........: cyrek- at -bigfoot.com -- Remove hyphens. Ta :)
URL...........: http://www.bigfoot.com/~cyrek/
Uncrackable...: "19.6A.23.38.52.73.45 25.31.1C 3C.53.44.39.58"

new topic     » goto parent     » topic index » view message » categorize

6. Re: routine_id help

On Tue, 16 Feb 1999, Roderick Jackson wrote:

] Hmmm, thanks for the advice (and thanks to Gabriel too.) This clears
] things up, but also hinders what I was doing.
]
] After posting, I experimented a bit; it indeed appears that "routine_id"
] can't access ANY forward routines, even in the same file. This doesn't
] sound right... the documentation states that forward access can be
] achieved, and even notes that mutual recursion is possible using the
] function. I had been working under the assumption that visibility was
] only limited by global/local/private scope.
]
] Just out of curiosity, if "routine_id" can't call a forward routine,
] how on earth can Euphoria implement mutual recursion? Perhaps my copy of
] refman.doc is out of date?
]
] Rod Jackson

I see your point, but I'm sure the reference manual that came with PD2.0
came with an example of mutual recursion (if somewhat messy IMHO).

Here's an example of forward routine calling:

constant Tiny = 3e-308 -- part of my example.

global integer second_id -- Eu equivalent of forward declaration

function first(atom y)
    return call_func(second_id, {x+1}) / (call_func(second_id, {x-1})+2)
 -- return second(x+1)/(second(x-1)+2)
end function

function second(atom y)
    return power(sin(y),2)
end function
second_id = routine_id("second") -- Finish up the forward declaration from
                                 -- before
atom number

number = 2.71828
? first(number) -- And it works!

I hope this furthers your understanding of the concept,
Carl

--
Carl R White -- Final Year Computer Science at the University of Bradford
E-mail........: cyrek- at -bigfoot.com -- Remove hyphens. Ta :)
URL...........: http://www.bigfoot.com/~cyrek/
Uncrackable...: "19.6A.23.38.52.73.45 25.31.1C 3C.53.44.39.58"

new topic     » goto parent     » topic index » view message » categorize

Search



Quick Links

User menu

Not signed in.

Misc Menu