[erlang-questions] Funargs: Ruby-like blocks for Erlang

Richard O'Keefe ok@REDACTED
Fri Jul 22 04:39:17 CEST 2011


On 22/07/2011, at 11:34 AM, Tony Arcieri wrote:

> On Thu, Jul 21, 2011 at 3:48 PM, Richard O'Keefe <ok@REDACTED> wrote:
> > However, I'd rather ask: can Erlang have something like Ruby like
> > blocks? Yes, yes it can.
> 
> Well yes, it does.  They are called funs.
> 
> This sort of sentiment lacks any sense of aesthetics.

On the contrary, it is motivated by a very strong sense of fitness.

> It is akin to saying that a potato sack is the same thing as a suit coat because both provide the same basic function of covering your torso.

No, it's like saying that a family sedan is pretty much the same
thing as a sports car with lots of extra chromium, several more
wheels spinning idly, and a red plastic nose on the front,

except that the family sedan is more useful.

It has been pointed out several times in this thread that funs
(or Smalltalk-style blocks) can be used easily in far more contexts
than Ruby "blocks".

> Let me call out explicitly what is ugly about Erlang fun syntax: it combines a symbolic token "->" with a keyword token "end" instead of a matching pair of symbolic tokens (e.g. "{" ... "}" or even "->" ... ".") or a matching pair of keywords (e.g. "do" ... "end").

The phrase "symbolic token" as used here is new to me.
One reason I don't like that phrase is that "->" "{" and "}"
are precisely *NOT* symbolic in any useful sense.  They are
just punctuation.  

In PL/I, x->y means "follow untyped pointer x to typed field y".
In C, x->y means "follow typed pointer x to typed field y".
In Strudel, x->y->z means "add an edge from x to z labelled y".
In Dot, x->y means "add an edge from x to y".
In logic, x->y means "y or not x".
In Pop-2 and S, x->y means "assign the value of y to the variable x".
In a Scheme library I have, (-> x y z...) means "send message y to
   the value of x with the values of z... as arguments."
In Prolog, P->Q;R means "if P then Q else R".

I could go on.  And don't get me started on possible meanings of {
and }.  (Take a look at J...)

You can *call* the punctuation marks "symbolic tokens" all you want,
the problem is that they aren't symbolic *OF* anything in particular,
except by arbitrary convention in a specific system of notation.

By the way, Erlang funs ***DON'T*** "combine a symbolic token with a
keyword token".  They use two keywords for brackets.
	The opening bracket is 'fun'.
	The closing bracket is 'end'.
The arrow is NOT a bracket.  You say it would be better to use "a
matching pair of keywords"?  Well, that's *EXACTLY* what Erlang does.

Funs use "->" to separate argument patterns and guards from bodies.
This is *consistent* throughout the whole of Erlang:
top level functions, if, case, receive, ...
EVERYTHING uses "->" to separate patterns-and-guards from bodies.

If you want to separate some patterns-and-guards from a body,
you use "->".  Nothing else.

If you see a "->", it is separating some patterns-and-guards from
a body.  Nothing else.

One of the things that is horrible about the proposal to add
funs-crippled-in-the-Ruby-manner to Erlang is that it BREAKS THIS
CONSISTENCY.  
	foobar do |X Y|
	   <body>
	end
*FAILS* to
 - enclose the arguments in parentheses, like ordinary functions
 - separate the arguments-and-guards from the body with "->",
   like ordinary functions.

This makes the language
  MORE complicated
  LESS consistent
for no real gain.

> I think there are a lot of people in the Erlang community who are either completely oblivious to how that sort of thing harms the aesthetics of the language or willfully choose to ignore it. This makes the Erlang "fun" syntax awkward and clumsy and not particularly "fun", when really anonymous functions are a powerful concept and should be a pleasure to use.

Smalltalk:
	[   :x :y   | some expression involving x and y]
Erlang:
	fun (X, Y) -> some expression involving X and Y end

Erlang uses matching keywords 'fun' 'end' instead of '[' ']'.
Erlang uses '(' and ',' instead of ':'.
Erlang uses ')' where Smalltalk has nothing, but then
  Erlang has pattern matching to fit in here.
Erlang uses '->' where Smalltalk has '|'.

OK, suppose we take advantage of Unicode,
and suppose we lose the argument parentheses.

	fun (X, Y) -> e(X, Y) end
=>
	〖X, Y ↣ e(X, Y)〗

We could do that easily, while still allowing the old syntax.

In a major revision of Erlang syntax, we should certainly give
serious thought to taking advantage of Unicode (without requiring it).

But just how much of a change is this, really?
Would that be enough to satisfy the Aubrey Beardsleys in the Erlang mailing list?

I've said it before:  Erlang syntax *is* clunky, but what merits reconsideration
(which might well ratify the existing syntax as a workable compromise) is the
*whole* language, not this part or that part.

This is a textbook example of a change that would make the language WORSE
by adding fragile support for a special case in a way that makes the language
*AS A WHOLE* less consistent, without adding ANY extra power, and very little
in the way of convenience.

As for me, I find fun ... end *clunky* but *tasteful*.
I know it could be done with fewer characters in other languages,
but it has been done in a way that is coherent with the rest of
the language, and such coherence is an aesthetic (and mnemonic!)
virtue not to be sacrificed lightly.

just one thing.  We can add new syntax for things we could not do easily
before, but


More information about the erlang-questions mailing list