[erlang-bugs] : bug

Richard Carlsson richardc@REDACTED
Fri Nov 30 11:41:21 CET 2007


Raimo Niskanen wrote:
> My first reaction to this was that maybe is_reference/1 is
> autoimported, but it would be nice and clear if the
> "fun is_reference/1" syntax did not do autoimporting. Easier to know
> what you get. You can use "fun erlang:is_reference/1" if so inclined.

Actually, the way it is working is precisely the way it should work,
even if some of you oldtimers may find it surprising that there
are parts of Erlang which actually are consistent from a language
design perspective. ;-)

Like I said, if a function (any function) is in scope, so that you can
call it like this:

     B = frobnicate(X)

then the form 'fun frobnicate/1' refers to the object (the function)
that the name frobnicate/1 is bound to, so:

     F = fun frobnicate/1,
     B = F(X)

should be exactly equivalent.

Note that the 'fun' keyword is not some kind of operator here - it's
just a syntactic modifier that tells the parser that what follows could
be a function name. You can't write just 'frobnicate/1' in Erlang,
because it would look like an atom divided by a number. But actually,
what we have here is a *variable* - a symbol ('fun f/N') bound to a
value (the function).

In a language like ML or Haskell, or even C, you simply have the
following corresponding equivalence:

     b = frobnicate(x)

is the same as

     f = frobnicate,
     b = f(x)

which surprises no one. But it is exactly the same thing that is
happening in Erlang - the only difference is that you have to say
'fun frobnicate/1' because function names (variables) are composed
syntactically of an atom and an integer

What *would* be strange, is if you'd decide that for _some_ names
(the automatically imported ones), that equivalence would not hold!

     /Richard





More information about the erlang-bugs mailing list