Re: routine_id()
- Posted by Robert Craig <rds at EMAIL.MSN.COM> Nov 12, 1997
- 655 views
Lee woo seob writes: > i'm now in vague on what the use of "routine_id()" is. i came to know > that it may be used to call the suroutines declared *after* the current > call. > however, in this case, the global variables must be declared and are > to be > set using "routine_id()" by the user, manually... > i guess there is some good reasons in why Rob made it as it is... > since if the > main use of routine_id() is only for calling coming-later subs, Rob > would > make ex.exe internally declare and set those variables. I expect that fewer than 10% of Euphoria programs will use routine_id(). But it does have some interesting uses: 1. It lets you call routines that come later in the program. This solves the "mutual recursion" recursion problem, and allows you to write a recursive-descent parser. 2. You can create a sequence of routine ids and index into it, rather than having a long chain of elsif's. Indexing will be much faster than having a long chain. This is useful in the inner loop of an interpreter. David Cuny has used this technique already to speed up an interpreter he is writing. 3. You can pass the routine id of a compare function to a general sorting routine (see csort.ex demo), or maybe a routine that "walks" through a data structure applying a function to each node. 4. An object-oriented "object" is supposed to consist of data plus the code needed to manipulate that data. Suppose you stored the routine id of a routine that will manipulate the data along with the data itself, in a sequence. You could set up a very object-oriented system. Version 2.0 alpha introduces the concept of a routine id anyway as part of linking to WIN32 C routines, so it seemed like a good time to add it for Euphoria routines. Why does routine id("xxx") only search *earlier* in the program for xxx? This was done to eliminate possible ambiguity and confusion. It does not restrict you, since you can compute the routine id later in your program, and assign it to a global variable that is declared at the very beginning of your program. Imagine if you write a .e file that wants to use the routine id of your (non-global) function foo(). Suppose the user of your .e file also has a routine named foo. If routine_id() were defined to pick up the currently-defined routine foo, you might accidentally pick up the routine id of *his* foo. After 3 days of debugging he might figure out that you are sometimes calling *his* foo. As routine_id is actually defined, if you really want to call his foo you can do it, but he must explicitly call routine_id("foo") and pass you the routine id himself. He will never fear that you are calling his routines without his permission. Regards, Rob Craig Rapid Deployment Software