# Meta-programming

**URL:** <https://community.zeek.org/t/meta-programming/2605>\
**Category:** Development\
**Tags:** development\
**Created:** [March 21, 2013, 3:54pm UTC](https://community.zeek.org/t/meta-programming/2605 "2013-03-21T15:54:23Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Seth\_Hall3](https://avatars.discourse-cdn.com/v4/letter/s/d6d6ee/32.png) [@Seth\_Hall3](https://community.zeek.org/u/Seth_Hall3)\
**Post date:** [March 21, 2013, 3:54pm UTC](https://community.zeek.org/t/meta-programming/2605/1 "2013-03-21T15:54:23Z")

</div>

Let me start by saying that I have an actual use case for this in the measurement framework and I'm not just messing around. 🙂

function add\_x(x: count): function(a: count): count  
&nbsp;&nbsp;{  
&nbsp;&nbsp;local tmp = function(a: count): count  
&nbsp;&nbsp;&nbsp;&nbsp;{  
&nbsp;&nbsp;&nbsp;&nbsp;# The 'x' variable ends up receiving the value of the same arity as my inner  
&nbsp;&nbsp;&nbsp;&nbsp;# defined function instead of the value of the outer scope where 'x' is.  
&nbsp;&nbsp;&nbsp;&nbsp;return a+x;  
&nbsp;&nbsp;&nbsp;&nbsp;};  
&nbsp;&nbsp;return tmp;  
&nbsp;&nbsp;}

event bro\_init()  
&nbsp;&nbsp;{  
&nbsp;&nbsp;local my\_adder = add\_x(5);  
&nbsp;&nbsp;print my\_adder(1); # hopefully would print 6, but it prints 2

&nbsp;&nbsp;local your\_adder = add\_x(100);  
&nbsp;&nbsp;print your\_adder(2); # hopefully would print 102, but it prints 4  
&nbsp;&nbsp;}

Would this functionality working like I wish it did be difficult? It would make a very nice usability feature for the measurement framework when setting thresholds.

&nbsp;&nbsp;.Seth

---

<div class="post-metadata">

**Author:** ![Vern](https://yyz1.discourse-cdn.com/flex011/user_avatar/community.zeek.org/vern/32/630_2.png) [@Vern](https://community.zeek.org/u/Vern)\
**Post date:** [March 22, 2013, 5:25pm UTC](https://community.zeek.org/t/meta-programming/2605/2 "2013-03-22T17:25:45Z")

</div>

> Would this functionality working like I wish it did be difficult?

First, as best as I can figure, the current functionality is seriously  
broken. It's reaching into a interpreter stack frame at the offset  
associated with x. That just happens to be the offset associated with 'a'  
during your my\_addr()/your\_addr() invocations, which is why the output is  
a doubling of 'a'.

Indeed, I've confirmed that if your outer function had an additional  
argument that comes before 'x', then the my\_addr()/your\_addr() invocations  
crash.

To fix this, it makes sense that for functions defined inside other  
functions, there's a clear binding model. The only trick is what  
that model should be. If it's bind-on-function-creation, then you'd  
get the semantics you desire. But I suspect there are other uses  
for such interior funtions where bind-on-application is handy - \*providing\*  
of course that the interior function doesn't escape to the exterior like  
it does in your example.

My proposal would be to introduce bind-on-function-creation semantics,  
and if later we find we sometimes want bind-on-application semantics,  
introduce that using an attribute.

But there's still the "would this .... be difficult" question. I suspect  
it's going to be messy. I'm having a hard time seeing how it doesn't  
involve a full parse-tree traversal of the interior function's body during  
creation. (And I leave the problem of the interior function itself having  
an interior function as an exercise for the reader. 🙂

&nbsp;&nbsp;&nbsp;&nbsp;Vern

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/zeek/original/1X/f09d732bc2cc7c7cc7e35db67cf4e1d5233ce7a7.png) [@system](https://community.zeek.org/u/system)\
**Post date:** [May 6, 2022, 3:40pm UTC](https://community.zeek.org/t/meta-programming/2605/3 "2022-05-06T15:40:51Z")

</div>

Closed on May 6, 2022, 3:40pm UTC


