Re: routine_id

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

Hi Glen,
just got home from work an see that no-one has responded yet, so I'll have a
go.

"routine_id" takes the name of a previously defined function or procedure,
and returns an integer. This integer represents an index into some hidden
internal list that Euphoria keeps. This list has all the memory addresses
(or something that can equate to an address) of the function/procedures you
have defined. The purpose of routine_id(), is so that you can call a
function or procedure, without knowing its name. To do this, you use the
integer returned by routine_id as a parameter to call_func() or call_proc()
as the case may be. These two routines, call your original routine
indirectly.

Ok, now for an example...

  integer theFunc

  function ABC( integer a, integer b)
    return a * b
  end function

  theFunc = routine_id("ABC")

  -- Calculate 5 times 6 = 30
  result = call_func(theFunc, {5,6})

----------
Now why is this a good idea? Seeing you can do the same without
routine_id()!

Well, consider this example...

  sequence theFunc
  theFunc = {0,0,0,0}

  function doMaths(integer type, integer first, integer second)
      -- Perform some math operation on 'first' and 'second'
    return (call_func(theFunc[type], {first,second}))
  end function

  function Mult( integer a, integer b)
    return a * b
  end function

  function Divn( integer a, integer b)
    return a / b
  end function

  function Add( integer a, integer b)
    return a + b
  end function

  function Sub( integer a, integer b)
    return a - b
  end function

  theFunc[1] = routine_id("Add")
  theFunc[2] = routine_id("Sub")
  theFunc[3] = routine_id("Divn")
  theFunc[4] = routine_id("Mult")


  -- Calculate 5 times 6 = 30
  result = doMath( 4, 5, 6)

  -- Calculate 5 - 6 = -1
  result = doMath( 2, 5, 6)

  -- Calculate 5 / 6 = 0.833333333
  result = doMath( 3, 5, 6)

  -- Calculate 5 + 6 = 11
  result = doMath( 1, 5, 6)

-------------------
The difference between the examples is that in the second, the function
doMath() was defined before the Add,Sub,Divn,Mult functions, but could still
run them indirectly via the "theFunc" value. Without using routine_id, this
would be impossble in Euphoria (though not in a lot of other languages).

Among other things, this technique allows you to write generic routines
whose behaviour is determined at runtime, and not compile time. It's also a
workaround for Euphoria's "define before use" philosophy.

In Win32lib, each of the permissible event handler type, such as onClick,
onClose, etc... is represented as a list of routine_ids. Each list, onClick
for example, has one entry for each control or window created. To start
with, these are set to -1, but when you set one via a call to routine_id(),
it tells Win32lib where your specific handler is. Win32lb then uses
call_proc() to invoke your handler at the appropriate times. Thus your
example,

> onClick[Button2] = routine_id( "onClick_Button2" )

is telling Win32lib, that the control Button2 has a handler routine that
your have written. Win32lib doesn't know what your routine is called, but it
can still access it via the routine_id value.

Another major use of routine_id is to enable you to pass the address of your
routine to some external 'C' code so that the 'C' code can call your
routine. This is used in Window callback processing.

All the OO variants of Euphoria take advantage of routine_id. Also, the
custom_sort() routine, supplied by RDS, uses a routine_id to allow you to
define your own comparision routine.

-------
cheers,
Derek.

----- Original Message -----
From: "Glen Brown" <gbrown7777 at YAHOO.COM>
To: <EUPHORIA at LISTSERV.MUOHIO.EDU>
Sent: Tuesday, September 19, 2000 6:59 PM
Subject: routine_id


> Hello all,
>
> Just one quick question.
>
>
> What EXACTLY does 'routine_id' do?
>
>
> And please don't tell me to RTM.  I have RTM an it says:
>
> Syntax: i = routine_id(st)
> Description: Return an integer id number, known as a routine id, for a
> user-defined Euphoria procedure or function. The name of the procedure or
> function is given by the string sequence st. -1 is returned if the named
> routine can't be found.
> Comments: The id number can be passed to call_proc() or call_func(), to
> indirectly call the routine named by st.
>
> That doesn't really tell me what it does.  It tells me what I get when I
use
> it.
>
> As an example:
>
> onClick[Button2] = routine_id( "onClick_Button2" )
>
> I know that 'onClick_Button2' is a procedure that is previously defined.
>
> Apparently 'onClick' is a sequence and I am making some subscript of that
> sequence equal to whatever number 'routine_id' spits out.
>
> I think that 'onClick' is probably defined in win32lib.  Don't know for
> sure.
>
> Shoot, for all I know I really want to know what "onClick[Whatever]" does
or
> perhaps "onKeyPress[Whatever]"  I really don't know.
>
> But for now.
>
>
> What EXACTLY does 'routine_id' do?
>
> Thank in advance for the several dozen answers I am sure to get from this.
>
> Glen
>
>
> _________________________________________________________
> Do You Yahoo!?
> Get your free @yahoo.com address at http://mail.yahoo.com
>

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

Search



Quick Links

User menu

Not signed in.

Misc Menu