Subj : Re: anyone know of a language where nonexistent functions return no error? To : comp.programming From : gswork Date : Fri Jul 22 2005 10:23 am Jonathan Bartlett wrote: > pantagruel wrote: > > An existent function calling such a nonexistent function would > > basically be left with an empty string, unless the programmer defined > > specific error generation. Theoretical literature welcome. hope this is > > a reasonable question to ask here > > > > Perl can do this and more (it can actually dynamically generate the > given function at runtime). If you define AUTOLOAD, it will be called > for non-existant functions in the current package. So if you have a > program like this: > > sub AUTOLOAD > { > return ""; > } > > $test = foo(); > > This does what you want. Even better, you can have stuff like this > (adapted from the Perl documentation): > > sub AUTOLOAD > { > my $name = our $AUTOLOAD; > *$AUTOLOAD = sub { return "You were calling $name\n"; } > goto &$AUTOLOAD; > } > > $a = foo(); #$a now has "You were calling foo\n" > $b = eee(); #$b now has "You were calling eee\n" > $c = foo(); #same as above, but now that foo() is loaded, it doesn't > call autoload any more the question that came to my mind was "yes, interesting, but how is it useful?" It potentially creates the misspelled function problem (instead of falling down it just accepts and masks the misspelling creating potentially subtle bugs) If using it as a throwaway function to fill out a rough framework for an app then the same could be achieved by using a generic stub function, e.g. stub() { return "stub"; } Only works in weakly typed langauges, though the perl idea could be applied to a strongly typed language and made to return something apropriate, e.g. int a=eee() returns the integer 0, string a=eee() returns "" I suppose it might be handy to create a function name that seems appropriate and then go create the proper function later on, but in any complex project that will lead to bugs, things being forgotten etc, and a bunch of well named stub functions woulod do anyway. So i'm intrigued now - what do folks use the feature for? .