Doubt about funs

Richard Carlsson <>
Wed Feb 21 09:58:13 CET 2001


On Tue, 20 Feb 2001, Jim Larson wrote:

> >1), it is apparent that `m' is the target of a call (so e.g. tools like
> >`xref' can know about it, and you can easily grep for `m:' in your source
> >code. In general, avoid passing around module names as data. (For the same
> >reason, it is better to use spawn/1 and spawn/2 than the old spawn/3 and
> >spawn/4, if possible. Avoid `apply/3'.)
> 
> Do you also advise against the use of callback interfaces? If so,
> how to you implement the desired dynamic dispatch? Pass funs to an
> initialization function?

No, sometimes a dynamic lookup is definitely what you want, but this can
be more neatly expressed with the normal remote-call syntax:

	f(Mod) ->
	    receive
		Pattern1 -> Mod:f(...);
		Pattern2 -> Mod:g(...);
		Pattern3 -> Mod:h(...);
	        ...
	    end

Calling `apply/3' is not necessary unless you are writing something like
an interpreter, where even the number of arguments in each call could vary.

(Using Mod:f(...), where Mod is not a constant atom, makes it impossible
for xref to know about the call, but at least you can grep for ":f(".)

My main point was just against the use of pseudo-functional values
`{M, F}' which do not have the same semantics as real functional values
(with respect to arity and dynamic lookup), not against dynamic lookup in
general.

	/Richard Carlsson


Richard Carlsson ()   (This space intentionally left blank.)
E-mail: 	WWW: http://www.csd.uu.se/~richardc/




More information about the erlang-questions mailing list