1. Why use routine_id?

When define a function or procedure, its name is also given.
It's better to refer to the routine name directly. 

My favorite languages:
C#, Euphoria, Haskell, Curry

new topic     » topic index » view message » categorize

2. Re: Why use routine_id?

Let's say you have 2 fuctions, foo() and bar().
However, foo() is part of a library, and bar() will be created by the user (such
as in
win32lib), and foo() must call bar(), but bar has not beened defined yet!

integer bar_id
    bar_id = 0

function foo( integer bar_id )

    -- we need to call bar
    call_proc( bar_id, {} )

end function


function bar()

    -- this function is designed by the user

end function
bar_id = routine_id("bar")


The main idea here is functionality. Since we have no GOTO command, nor line
numbers or names to
jump to, we sometimes need to be able to break the natual order of the program.
routine_id()
allows you to do that rather easily. Also, routine_id() allows you to call a
routine if you have
no idea what its name is. Just use Win32Lib and you'll understand...

~Greg

----- Original Message -----
From: Liu Junfeng <rufi at 163.com>
To: EUforum <EUforum at topica.com>
Sent: Thursday, March 20, 2003 10:34 PM
Subject: Why use routine_id?



When define a function or procedure, its name is also given.
It's better to refer to the routine name directly.

My favorite languages:
C#, Euphoria, Haskell, Curry



TOPICA - Start your own email discussion group. FREE!

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

3. Re: Why use routine_id?

----- Original Message -----
From: "Liu Junfeng" <rufi at 163.com>
To: "EUforum" <EUforum at topica.com>
Subject: Why use routine_id?


>
> When define a function or procedure, its name is also given.
> It's better to refer to the routine name directly.

Euphoria has the rule: Code cannot refer to any identifier unless that
identifier is defined earlier in the program.

This means that if a program wants to run a routine that is defined further
down in the program source code, you must either move the routine to an
earlier position in the file, or use routine_id. The idea is that routine_id
is located after the routine who's id you are getting, and assigning the id
to a variable that is defined earlier in the code.

 integer id

 procedure aaa()
  call_proc(id,{})
 end procedure

 procedure xxx()
 end procedure

 id = routine_id("xxx")

 aaa()

The main use of routine_id is to allow you write generic routines, and pass
routine_ids as parameters. The Euphoria library routine customer_sort() uses
this technique to allow you to sort any type of data item.


----------------
cheers,
Derek Parnell

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

4. Re: Why use routine_id?

----- Original Message -----
From: "Liu Junfeng" <rufi at 163.com>
To: "EUforum" <EUforum at topica.com>
Subject: RE: Why use routine_id?


[snip]

> I think the main reason is that Eu doesn't have a function type.
> In routine_id, a function is really passed as a sequence, and return a
> integer. I've just forgotten this point.

What *ARE* you talking about. This makes no sense at all!

Euphoria does have a function type!

 function aaa()
  return 1
 end function

 a = routine_id("aaa")

 x = call_func(a,{})


The NAME of a function/procedure is passed as a parameter to routine_id()
and that returns an integer. The integer is an index into an internal table
of routines that the program knows about.

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

5. Re: Why use routine_id?

On Fri, 21 Mar 2003 21:01:50 +1100, Derek Parnell
<ddparnell at bigpond.com> wrote:

>
>What *ARE* you talking about. This makes no sense at all!
>
>Euphoria does have a function type!

I think he meant a type like integer, atom, sequence or object which
specifically holds a function; routine_id accepts a sequence (not a
function or procedure) as parameter and returns an integer.

This means the interpreter does not validate the sequence actually is
a function or procedure (routine_id("jibberish") quietly returns -1)
and there is no way to indicate/check the routine, say, accepts 3
parameters (oft clobbered on that one migrating code to setHandler).

Occasionally I wrap routine_id as follows to catch typos:

function rtn_id(sequence name)
integer f
	f=3Droutine_id(name)
	if f=3D-1 then ?9/0 end if	-- typo in name
	return f
end function


Euphoria sacrificed forward references on the altar of speed (and I
guess to simplify things internally), a compromise I can accept.


Pete

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

Search



Quick Links

User menu

Not signed in.

Misc Menu