lua-users home
lua-l archive

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


okay, I've got to digest those.

On Mon, Oct 6, 2014 at 3:18 PM, Andrew Starks <andrew.starks@trms.com> wrote:


On Mon, Oct 6, 2014 at 7:50 AM, Charles Smith <cts.private.yahoo@gmail.com> wrote:
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




I think that this is what you want:

```
local msg_union = {}

function msg_union:alt1(body)
  assert(self == msg_union)
  assert(self.alt2)
-- stuff
end

function msg_union:alt2(body)
--stuff
end

msg_union:alt() --does not fail

```

Am i missing something?

-Andrew