fun question...

Dave Smith <>
Tue Mar 30 19:23:55 CEST 2010


I have a scenario where I have a atom bound to a variable,  and I want to
construct a fun using this variable as the module name.   This is what I
have...

encode(CodecModule, CodePoints) when is_atom(CodecModule) ->
   encode(*fun(C) -> CodecModule:endode(C) end*, CodePoints);
encode(EncodeFn, CodePoints) ->
   {ok, list_to_binary(lists:map(EncodeFn, CodePoints))}.


This works fine, but I was expecting to be able to something like this...

encode(CodecModule, CodePoints) when is_atom(CodecModule) ->
   encode(*fun CodecModule:endode/1*, CodePoints);
encode(EncodeFn, CodePoints) ->
   {ok, list_to_binary(lists:map(EncodeFn, CodePoints))}.



I can call the function directly using the variable like this...
Bin = CodecModule:encode(CodePt).

So I was expecting this to work...
EncFn = fun  CodecModule:encode/1.

Can someone clarify?  Is this second form expanded at compiled time?
Is the first example above the best approach?



--DS


More information about the erlang-questions mailing list