RE: Declaring a Function Before it is used
- Posted by Matt Lewis <matthewwalkerlewis at yahoo.com> Oct 31, 2003
- 476 views
Robert Craig wrote: > > I still think the advantages of > "define-it-before-you-use-it" outweigh > the nuisance of occasionally having to copy/paste > a routine to a new place. It may not be desirable > in *every* program, but when people know there are > no exceptions to this rule, it promotes the > readability and maintainability of Euphoria > programs in general. > Of course, we've entered the realm of flame/holy war here, so I can't possibly resist putting in my own comments. I think that "define-it-before-you-use-it" is not as unequivocal as it first looks. For instance, there is much confusion regarding routine_id(), in that the function must be defined at compile time, rather than run time. I'd say that Euphoria is really "define-it-before-you-write-it," at least from a run-time, program flow perspective. I'd personally be happy if we could simply use run-time definitions. If I get enough time, I'll try to put this into my interpreter this weekend, but here's a rough sketch of my idea: * At compile time, note any routine usage that isn't already defined (probably using a linked list or a hash table or some combination) * When required, do a symbol search on the *entire* visible scope, not restricting to only previously defined in the source * As each routine is encountered in the source, check the above list to verify correct usage, etc * Undefined routines become warnings at the end if not encountered, or if encountered at run-time, are errors. This is because you'll never know if the routine might be defined at the next line until you get to the end, and shouldn't require a major overhaul of the parser, since you're still doing it all in one pass. This would also allow forward-in-scope routine_id() calls. The emphasis on run-time seems appropriate for an interpreter to me (and somewhat more intuitive). I don't really see the benefit in prototypes. If you have prototypes, you have to go look somewhere else for the declaration. If you have to go look somewhere, why make two places to look instead of one? Matt Lewis