[erlang-questions] Parse-transforming !! to a function call

David Mercer dmercer@REDACTED
Thu Aug 16 18:10:21 CEST 2007


On 8/16/07, Joe Armstrong <erlang@REDACTED> wrote:
> For things that are used a lot infix syntax is nice
>
>      A + B is nicer than plus(A, B)

The problem with infix is formatting.  How do you format in your editor a
long list of infix-separated expressions?

	A
	+ B
	+ C
	+ D

Loses the fact that A has an equivalent rank to B, C, D.  If later, you
decide to reorder your expressions, A cannot be freely reordered like the
other lines because it lacks the infix operator.  Of course you can move the
infix operator to the end (this is what is usually done when the infix
operator is a comma), but then expression D loses its free movement ability.
Keeping with infix, we could put the operator on its own line:

		A
	+
		B
	+
		C
	+
		D
Or

	A
		+
	B
		+
	C
		+
	D

Which enables you to move A around so long as you take its following
operator along with it and so long as it is not going at the end.

What I am getting at is that prefix operator make it much easier to format
your code.  I'm not a Lisp programmer, but that is one of the first things I
noticed about Lisp, which I like.  This is much easier to rearrange:

	+
		A
		B
		C
		D

Even on a single line:

	+ A B C D

Is easier to rearrange than its infix counterpart:

	A + B + C + D

Since A cannot easily be moved to the end.  One would have to move the "A +
" to the end, and then move the " + " between the "D" and "A".

>      A + B is nicer than plus(A, B)

I would argue that you have simply exchanged one infix operator (plus) for a
another (comma) and added in a prefix ("plus(") and postfix (closing paren)
to boot, so, yes, you are right, one infix operator is better than an infix
operator that also requires a prefix and postfix.

Erlang is by no means the only language to have this problem.  Indeed, I
think any non-Lisp language has this problem.  However, in some languages
the problem is not quite so severe.  Erlang's syntax, however, is as bad as
SQL's in terms of formatting, in my opinion: I never know exactly how to
format my Erlang consistently.

Example 1: Take a simple function definition:

	f(...) -> ... .

Seems simple enough.  If the body is long, we can format it like so:

	f(...) ->
		... ,
		... ,
		... .

Now let's add in a guard:

	f(...) when ... ->
		... ,
		... ,
		... .

How about a complicated guard:

	f(...)
		when
			..., ..., ...; 
			..., ..., ...; 
			..., ..., ...; 
		->
			... ,
			... ,
			... .

Now the body of our function has to be indented another level to show that
it is subordinate to the infix operator "->".  But if we later decide to
change the function to eliminate the guard, we have to modify our
indentation, too.

Example 2: Multiple clauses in a function.  They are separated by the infix
semicolon operator, but terminated by a postfix period.  This arrangement
results in the ordering problems that was described earlier with our A + B +
C + D example: we cannot freely rearrange the order of the clauses without
making structural changes to our code.  Note that this would not be a
problem if the semicolon were a postfix clause terminator, but since it is
infix, it is.  If it were postfix, then this would be valid, and we wouldn't
have the clause reordering problem:

	f(...) -> ...;
	f(...) -> ...;
	f(...) -> ...;
	f(...) -> ...;
	. % Postfix function terminator

Therefore, I would argue for infix operator reduction rather than expansion.

> For things that are used a lot infix syntax is nice

I don't find infix operators nice at all.  I realize we are stuck, by and
large, with Erlang's infix-heavy syntax, however, so I'll go along with it.

Cheers,

David




More information about the erlang-questions mailing list