]> git.lizzy.rs Git - metalua.git/blob - doc/manual/syntax-ext.tex
spelling fixes to the documentation
[metalua.git] / doc / manual / syntax-ext.tex
1 \section[Metalua extensions]{Metalua syntax extensions over Lua}
2 Metalua is essentially Lua + code generation at compile time +
3 extensible syntax. However, there are a couple of additional
4 constructs, considered of general interest, which have been added to
5 Lua's original syntax. These are presented in this section.
6
7 \subsection{Anonymous functions}
8 Lua lets you use anonymous functions. However, when programming in a
9 functional style, where there are a lot of short anonymous functions
10 simply returning an expression, the default syntax becomes
11 cumbersome. Metalua being functional-style friendly, it offers a
12 terser idiom: ``{\tt function(arg1, arg2, argn) return some\_expr
13   end}'' can be written:\\
14 ``{\tt|arg1,arg2,argn| some\_exp}''.
15
16 Notice that this notation is currying-friendly, i.e. one can easily
17 write functions that return functions: ``{\tt function(x) return
18 function(y) return x+y end end}'' is simply written ``{\tt|x||y|
19 x+y}''.
20
21 Lua functions can return several values, but it appeared that
22 supporting multiple return values in metalua's short lambda notation
23 caused more harm than good. If you need multiple returns, use the
24 traditional long syntax.
25
26 Finally, it's perfectly legal to define a parameterless function, as
27 in {\tt | | 42}. This makes a convenient way to pass values around in a
28 lazy way.
29
30 \subsection{Functions as infix operators}
31
32 In many cases, people would like to extend syntax simply to create
33 infix binary operators. Haskell offers a nice compromize to satisfy
34 this need without causing any mess, and metalua incorporated it: when
35 a function is put between backquotes, it becomes infix. for instance,
36 let's consider the {\tt plus} function ``{\tt plus=|x,y|x+y}''; this
37 function can be called the classic way, as in ``{\tt plus (20, 22)}''; but
38 if you want to use it in an infix context, you can also write ``{\tt
39 20 `plus` 22}''.
40
41 \subsection{Algebraic datataypes}
42
43 This syntax for datatypes is of special importance to metalua, as it's
44 used to represent source code being manipulated. Therefore, it has its
45 dedicated section later in this manual.
46
47 \subsection{Metalevel shifters}
48
49 These two dual notations are the core of metaprogramming: one
50 transforms code into a manipulable representation, and the other
51 transforms the representation back into code. They are noted
52 {\tt+\{...\}} and {\tt-\{...\}}, and due to their central role in
53 metalua, their use can't be summed up adequately here: they are fully
54 described in the subsequent sections about metaprogramming.