user-defined operators

Thomas Lindgren thomasl_erlang@REDACTED
Sun Mar 21 18:06:56 CET 2004

Some time ago there was interest in extending Erlang
with your own operators, e.g., !!. So why not provide

Here is a code sketch for extending Erlang to permit
simple user-defined infix operators `op`. The idea is
basically taken from Haskell and seems fairly clean. I
used backquotes around the operator because Haskell
did so.

I modified the erlang parser and scanner to support
this notation; it seems to work, though I haven't used
it very extensively. 

f(X, Y) -> X `foo` Y.   %% note: foo is "backquoted"

is parsed as if:

f(X,Y) -> 'foo'(X,Y).   %% call function foo/2

The operator `op` has the same precedence as
andalso/orelse. You can also write `mod:func` to
invoke mod:func(X,Y), e.g.,

   Port `erlang:port_command` Data

Both the module and the function have to be non-empty
atoms. Thus, the following are considered errors,
where foo is any atom:

I haven't tested how the scanner behaves with extreme
quoting in the infix operator. The error return value
in the scanner does not have a format_error. Finally,
the code could probably be improved by someone more
familiar with erl_scan/erl_parse.

Example use:
1> {ok, Ts, _} = erl_scan2:string("f(X,Y) -> X `op`
2> erl2:parse_form(Ts).


Do you Yahoo!?
Yahoo! Finance Tax Center - File online. File on time.
This communication is confidential and intended solely for the addressee(s). Any unauthorized review, use, disclosure or distribution is prohibited. If you believe this message has been sent to you in error, please notify the sender by replying to this transmission and delete the message without disclosing it. Thank you.

E-mail including attachments is susceptible to data corruption, interruption, unauthorized amendment, tampering and viruses, and we only send and receive e-mails on the basis that we are not liable for any such corruption, interception, amendment, tampering or viruses or any consequences thereof.

-------------- next part --------------
A non-text attachment was scrubbed...
Name: erl2.yrl
Type: application/octet-stream
Size: 22457 bytes
Desc: erl2.yrl
URL: <>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: erl_scan2.erl
Type: application/octet-stream
Size: 29860 bytes
Desc: erl_scan2.erl
URL: <>

More information about the erlang-questions mailing list