user-defined operators

Richard A. O'Keefe ok@REDACTED
Thu Mar 25 23:36:32 CET 2004

I'm seeing a certain amount of scaremongering in this thread.

First, the thread is about user-defined operators, NOT about
operator overloading.  Whatever the merits and pitfalls of
operating overloading, they are not relevant to the topic of
user defined operators.

Second, most functional languages have supported user defined
operators for years.  ML has them.  Haskell has them.  Clean
has them.  Prolog has them.  Mercury has them.

*DO* you in fact get a welter of confusing operators?


In part this is because operators can only have one or two arguments,
while functions and predicates commonly need more.

Haskell has two kinds of operators: alphabetic ones (which must always
be enclosed in backquotes) and symbolic ones (which may never be
enclosed in backquotes).  So in Haskell `elem` is a alphabetic operator
and `!!` is not legal syntax at all.

Operators are declared using
	infix  [Prec] op...
	infixl [Prec] op...
	infixr [Prec] op...
where Prec is a decimal digit.  A symbolic operator may only be used if
it has been declared.  An alphabetic operator has the weakest precedence
unless it is explicitly declared.

Now alphabetic operators (especially if they all have the same precedence,
as they used to in Haskell) are no harder to read than any other function
name, and no more subject to overloading.  (Each operator/function in
Haskell has precisely one meaning in scope.)

It does not seem to me that
    n `elem` primes
    seconds `div` 60
    fromInteger big `asTypeOf` x
    expr `catch` handler
are particularly hard to read.

As for the idea that some group of experts should determine a set of
operators for Erlang, well, (a) they already did, and (b) if the
experts who designed Haskell came up with gems like $! and :% (actually,
once you know the rule that a symbolic operator starting with a colon
must be a constructor, it's not that hard to figure out what :% does),
what reason is there to expect the Erlang experts to do any better?

The important thing about !! is not that it is spelled bang-bang, but
that it is a concise notation for doing a remote procedure call.

Suppose that alphabetic operators *only* were adopted, so that any
Erlang user could use any two-argument function as an infix operator.
Instead of

    Result = Pid !! Message

we'd have to write

    Result = Pid `rpc` Message

which is three characters longer but 100% clearer.

I note that Fortran has had user-defined operators for a while now;
the syntax is <dot><name><dot>, so the Fortran equivalent might be

    Result = Pid .RPC. Message

I haven't heard anything about an explosion of unreadability in Fortran
since this addition was made; maybe using words as operators really isn't
any worse than using words as function names.

More information about the erlang-questions mailing list