lua-users home
lua-l archive

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


On Fri, Feb 19, 2016 at 5:01 PM, Ivan <ivan@sleepyiora.pw> wrote:


And still I'm looking for a way to use both OOP style (my_array:length()) and array-style (my_array[1] = 0)



​Put an if-else in the "get" function such that if the argument is a number you do the array access, else you search the metatable. Something like:

static int get (lua_State *L) {
    array* a = check_array (L);
    if (lua_isnumber (L, 2)) {
      int index = lua_tointeger (L, 2);
      <... array access here ...>
      return 1;
    }
    else {
      lua_getglobal (L, "array");
      lua_pushvalue (L, 2);
      lua_gettable (L, -2);
      lua_remove (L, -2);
      return 1;
    }
  }