1 | <html> |
---|
2 | <head> |
---|
3 | <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> |
---|
4 | <title>Appendix A. Rationale for some of the design decisions</title> |
---|
5 | <link rel="stylesheet" href="boostbook.css" type="text/css"> |
---|
6 | <meta name="generator" content="DocBook XSL Stylesheets V1.69.1"> |
---|
7 | <link rel="start" href="index.html" title="The Boost C++ Libraries"> |
---|
8 | <link rel="up" href="lambda.html" title="Chapter 6. Boost.Lambda"> |
---|
9 | <link rel="prev" href="lambda/s09.html" title="Contributors"> |
---|
10 | <link rel="next" href="program_options.html" title="Chapter 7. Boost.Program_options"> |
---|
11 | </head> |
---|
12 | <body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"> |
---|
13 | <table cellpadding="2" width="100%"> |
---|
14 | <td valign="top"><img alt="boost.png (6897 bytes)" width="277" height="86" src="../../boost.png"></td> |
---|
15 | <td align="center"><a href="../../index.htm">Home</a></td> |
---|
16 | <td align="center"><a href="../../libs/libraries.htm">Libraries</a></td> |
---|
17 | <td align="center"><a href="../../people/people.htm">People</a></td> |
---|
18 | <td align="center"><a href="../../more/faq.htm">FAQ</a></td> |
---|
19 | <td align="center"><a href="../../more/index.htm">More</a></td> |
---|
20 | </table> |
---|
21 | <hr> |
---|
22 | <div class="spirit-nav"> |
---|
23 | <a accesskey="p" href="lambda/s09.html"><img src="images/prev.png" alt="Prev"></a><a accesskey="u" href="lambda.html"><img src="images/up.png" alt="Up"></a><a accesskey="h" href="index.html"><img src="images/home.png" alt="Home"></a><a accesskey="n" href="program_options.html"><img src="images/next.png" alt="Next"></a> |
---|
24 | </div> |
---|
25 | <div class="appendix" lang="en"> |
---|
26 | <div class="titlepage"><div><div><h2 class="title"> |
---|
27 | <a name="id2713626"></a>Appendix A. Rationale for some of the design decisions</h2></div></div></div> |
---|
28 | <div class="toc"> |
---|
29 | <p><b>Table of Contents</b></p> |
---|
30 | <dl><dt><span class="section"><a href="apa.html#lambda.why_weak_arity"> |
---|
31 | Lambda functor arity |
---|
32 | </a></span></dt></dl> |
---|
33 | </div> |
---|
34 | <div class="section" lang="en"> |
---|
35 | <div class="titlepage"><div><div><h4 class="title"> |
---|
36 | <a name="lambda.why_weak_arity"></a> |
---|
37 | Lambda functor arity |
---|
38 | </h4></div></div></div> |
---|
39 | <p> |
---|
40 | The highest placeholder index in a lambda expression determines the arity of the resulting function object. |
---|
41 | However, this is just the minimal arity, as the function object can take arbitrarily many arguments; those not needed are discarded. |
---|
42 | Consider the two bind expressions and their invocations below: |
---|
43 | |
---|
44 | </p> |
---|
45 | <pre class="programlisting"> |
---|
46 | bind(g, _3, _3, _3)(x, y, z); |
---|
47 | bind(g, _1, _1, _1)(x, y, z); |
---|
48 | </pre> |
---|
49 | <p> |
---|
50 | |
---|
51 | This first line discards arguments <code class="literal">x</code> and |
---|
52 | <code class="literal">y</code>, and makes the call: |
---|
53 | </p> |
---|
54 | <pre class="programlisting"> |
---|
55 | g(z, z, z) |
---|
56 | </pre> |
---|
57 | <p> |
---|
58 | whereas the second line discards arguments <code class="literal">y</code> and |
---|
59 | <code class="literal">z</code>, and calls: |
---|
60 | </p> |
---|
61 | <pre class="programlisting"> |
---|
62 | g(x, x, x) |
---|
63 | </pre> |
---|
64 | <p> |
---|
65 | In earlier versions of the library, the latter line resulted in a compile |
---|
66 | time error. |
---|
67 | |
---|
68 | This is basically a tradeoff between safety and flexibility, and the issue |
---|
69 | was extensively discussed during the Boost review period of the library. |
---|
70 | The main points for the <span class="emphasis"><em>strict arity</em></span> checking |
---|
71 | was that it might |
---|
72 | catch a programming error at an earlier time and that a lambda expression that |
---|
73 | explicitly discards its arguments is easy to write: |
---|
74 | </p> |
---|
75 | <pre class="programlisting"> |
---|
76 | (_3, bind(g, _1, _1, _1))(x, y, z); |
---|
77 | </pre> |
---|
78 | <p> |
---|
79 | This lambda expression takes three arguments. |
---|
80 | The left-hand argument of the comma operator does nothing, and as comma |
---|
81 | returns the result of evaluating the right-hand argument we end up with |
---|
82 | the call |
---|
83 | <code class="literal">g(x, x, x)</code> |
---|
84 | even with the strict arity. |
---|
85 | </p> |
---|
86 | <p> |
---|
87 | The main points against the strict arity checking were that the need to |
---|
88 | discard arguments is commonplace, and should therefore be straightforward, |
---|
89 | and that strict arity checking does not really buy that much more safety, |
---|
90 | particularly as it is not symmetric. |
---|
91 | For example, if the programmer wanted to write the expression |
---|
92 | <code class="literal">_1 + _2</code> but mistakenly wrote <code class="literal">_1 + 2</code>, |
---|
93 | with strict arity checking, the complier would spot the error. |
---|
94 | However, if the erroneous expression was <code class="literal">1 + _2</code> instead, |
---|
95 | the error would go unnoticed. |
---|
96 | Furthermore, weak arity checking simplifies the implementation a bit. |
---|
97 | Following the recommendation of the Boost review, strict arity checking |
---|
98 | was dropped. |
---|
99 | </p> |
---|
100 | </div> |
---|
101 | </div> |
---|
102 | <table width="100%"><tr> |
---|
103 | <td align="left"></td> |
---|
104 | <td align="right"><small>Copyright © 1999-2004 Jaakko Järvi, Gary Powell</small></td> |
---|
105 | </tr></table> |
---|
106 | <hr> |
---|
107 | <div class="spirit-nav"> |
---|
108 | <a accesskey="p" href="lambda/s09.html"><img src="images/prev.png" alt="Prev"></a><a accesskey="u" href="lambda.html"><img src="images/up.png" alt="Up"></a><a accesskey="h" href="index.html"><img src="images/home.png" alt="Home"></a><a accesskey="n" href="program_options.html"><img src="images/next.png" alt="Next"></a> |
---|
109 | </div> |
---|
110 | </body> |
---|
111 | </html> |
---|