lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


I need to take the time to develop a clearer example, but I think the point is, alt1 and alt2 also use msg_union.  That's what I mean by recursive design.  It's a classic chicken/egg issue.

On Mon, Oct 6, 2014 at 2:35 PM, Coda Highland <chighland@gmail.com> wrote:
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