user-defined operators

Thomas Lindgren <>
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
them?

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:
`:foo` 
`foo:` 
`:`  
``

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`
Y.").
...
2> erl2:parse_form(Ts).
...

Best,
Thomas

__________________________________
Do you Yahoo!?
Yahoo! Finance Tax Center - File online. File on time.
http://taxes.yahoo.com/filing.html
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: <http://erlang.org/pipermail/erlang-questions/attachments/20040321/95184d04/attachment.obj>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: erl_scan2.erl
Type: application/octet-stream
Size: 29860 bytes
Desc: erl_scan2.erl
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20040321/95184d04/attachment-0001.obj>


More information about the erlang-questions mailing list