[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: Fwd: How to forward declare globals
- From: Coda Highland <chighland@...>
- Date: Mon, 6 Oct 2014 05:35:30 -0700
On Mon, Oct 6, 2014 at 5:30 AM, Charles Smith
<cts.private.yahoo@gmail.com> wrote:
> Recursive design.
>
>
> msg_union = {
> ["0"] = "alt1",
> ["1"] = "alt2",
> ...
> }
>
> function alt1 (body)
> ...
> function alt2 (body)
>
>
> ielen = _G[msg_union[tostring (selector)]] (buffer(offset), pinfo, tree)
>
> Maybe there's a better way? It looks like it's going to be slow.
>
>
> The msg_union has to be defined before it can be used by the (sub)msgs. But
> it needs access to the (sub)msgs.
Remember that functions are first-class citizens in Lua. Try this:
function alt1 (body)
...
function alt2 (body)
msg_union = {
[0] = alt1,
[1] = alt2
}
ielen = msg_union[selector](blah)
That is, you can pass around functions just like you can pass around
any other value. Tou can store them in tables.
For what it's worth, this is legal:
msg_union = {
[0] = function(body) ... end,
[1] = function(body) ... end
}
/s/ Adam