lua-users home
lua-l archive

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


Thanks for replies. I forgot to tell you that this is runtime behavior.

I like the idea of  adding thrid element, but the situtation is much more complicated in other seanarios where we need to do the calculations, comparisons to add the items.
 

Is it possible to add anonymous function to the table and through meta table, I can check the type of the element added to the table.
Enum1 = ENUMERATION {
    {1, "Forward"},
   {2, "Backword"},

   function()
      if BIDIERCTIONAL_SUPPORTED then
      return {3, "BiDirectional"}
  end,
 
  {4, "None" }
}

Any other approaches welcome. Thing is, it should be readable format.


On Wed, Nov 10, 2010 at 1:40 PM, steve donovan <steve.j.donovan@gmail.com> wrote:
On Wed, Nov 10, 2010 at 9:45 AM, Gopalakrishnan Subramani
<gopalakrishnan.subramani@gmail.com> wrote:
> Enum1 = ENUMERATION {
>     {1, "Forward"},
>    {2, "Backword"},
>
>    if BIDIERCTIONAL_SUPPORTED then
>       {3, "BiDirectional"},
>
>   {4, "None" }
> }
>
> If you see, the "Bidirectioanl" is added only if it is enabled. I don't
> think, Lua support statements within the table definition.

That is true, so you can't do it this way. But you could state it like
this by making your enumerations objects:

Enum1 = ENUMERATION()
Enum1:add {1,"Forward"}
Enum1:add{2,"Backward"}
if BIDIERCTIONAL_SUPPORTED then Enum1:add{3,"BiDirectional"} end
Enum1:add {4,"None"}

Which is not so pretty. So how about this?

Enum1 = ENUMERATION {
  {1, "Forward",true},
  {2, "Backword",true},
  {3, "BiDirectional",BIDIERCTIONAL_SUPPORTED},
  {4, "None",true}
}

And loop through the enumeration afterwards and remove fields where
the conditional entry is false/nil  - this can be done by the
ENUMERATION function directly.

steve d.