Re: "Forward" function declarations?
- Posted by Euman <euman at bellsouth.net> Feb 08, 2001
- 452 views
</snip> constant TRUE = 1, FALSE = 0 sequence types, handles, rps global procedure add_function (object handle, integer rp) integer pos pos = find (handle, handles) if pos then types[pos] = TRUE -- It does return something rps[pos] = rp else handles = append(handles, handle) types = append(types, TRUE) rps = append(rps, TRUE) end if end procedure global procedure add_procedure (object handle, integer rp) integer pos pos = find (handle, handles) if pos then types[pos] = FALSE -- It does return something rps[pos] = rp else handles = append(handles, handle) types = append(types, FALSE) rps = append(rps, FALSE) end if end procedure global function call_routine (object handle, sequence arg) integer pos pos = find (handle, handles) if pos then if types[pos] then return {TRUE, call_func (rps[pos], arg)} else call_proc (rps[pos], arg) return {TRUE} end if else return {FALSE} end if end function <snip\> I think Kat might have been refering to this. Euman ----- Original Message ----- From: "Kat" <gertie at PELL.NET> To: <EUforum at topica.com> Sent: Thursday, February 08, 2001 13:34 Subject: Re: "Forward" function declarations? > On 8 Feb 2001, at 10:13, Michael Nelson wrote: > > > > > ----- Original Message ----- > > From: "Ted Fines" <fines at macalester.edu> > > To: <EUforum at topica.com> > > Sent: Thursday, February 08, 2001 7:45 AM > > Subject: "Forward" function declarations? > > > > > > > I need to do the "C" equivalent of a "forward" function declaration in > > > Euphoria. Can this be done? > > > > > > > Not directly. A forward reference is illegal in Euphoria: > > > > funcion foo(object x) > > integer z > > z=bar(x) --syntax error "bar" not defined > > return z+2 > > end function > > > > function bar(object y) > > if atom(y) return -1 else return length(y) > > end function > > > > printf(1,"%d",foo({1,2,3})) > > > > and there is no way to declare a routine before it is defined (as there is > > in C). Instead Euphoria uses routine_id(): > > > > integer bar_ID --bar_ID must be defined before foo() > > > > funcion foo(object x) > > integer z > > z=call_func(bar_ID,{x}) > > return z+2 > > end function > > > > function bar(object y) > > if atom(y) return -1 else return length(y) > > end function > > > > bar_ID=routine_id("bar") -- routine_id() must be used after bar() > > > > printf(1,"%d",foo({1,2,3})) > > > > > > Now this works as foo() can "see" bar_ID and bar_ID is assigned a value > > before foo() is called. > > This is another example of where the interpreter can be made smarter: If the procedure > hasn't been declared yet, look further into the code for it!! I have seen some programs > wherein everything has been routine_id()'d, and all procedure and function calls are to > the routine_id . The interpreter could be doing this as a default, and making the order > of precedence irrelevant. > > Kat > >