Choking on Syntactic Sugar

Richard Carlsson <>
Wed Mar 14 12:52:25 CET 2001

On Wed, 14 Mar 2001, Hakan Stenholm wrote:

> >       A new construction is allowed in patterns, namely a literal
> >       string as the first operand of the ++ operator. Example: 
> >
> >             f("prefix" ++ L) -> ...
> >
> >       This is syntactic sugar for the equivalent, but harder to read
> >
> >             f([$p,$r,$e,$f,$i,$x | L]) -> ...
> >
> >No other non-constant uses of `++' are allowed.
> This is not quite true, any non-negative integer (not just 0-255)
> appears to work just fine and in this case it should be possible to
> check at compile time.
> [...]
> Wouldn't it also be reasonable to generalize this by alowing the
> string argument to be any kind of list of patterns (that might
> contain varibles and _) that is known at compile time ? Then again
> we don't gain anything from this (from what I can see) so there is
> no apprent reason to do this ?!

It would certainly be possible. My guess is that `[Int1, Int2,...] ++
Tail' is currently accepted as a special case only because it would be
odd if `"abc" ++ Tail' was legal, but `[$a, $b, $c] ++ Tail' was not.

Generalising this is simple, and might even be useful in some cases. You
could write stuff like:

	foo(?xx_prefix(X, Y, Z) ++ Tail, ...) -> {X, Y, Z, Tail}.

which is clearer than including the tail in the macro call:

	foo(?xx_pattern(X, Y, Z, Tail), ...) -> {X, Y, Z, Tail}.


Richard Carlsson ()   (This space intentionally left blank.)
E-mail: 	WWW:

More information about the erlang-questions mailing list