lua-users home
lua-l archive

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


Am Di., 22. Nov. 2022 um 12:47 Uhr schrieb bil til <biltil52@gmail.com>:
>
> PPS: Sorry, one further correction, now I wrote my MyLuaL_addmeta
> function like this, I want to post it here just to have it completed
> somehow...:
> ... (see my Nov 22 post)...

I just recognized one thing, which sounds slightly odd to me, but
which I MUST do, if I want to give the Lua user full flexibility with
the "colon syntactic sugar".

To use the com function, of course the Lua user first has to open a
com channel using my com function "com.open" (I skip the parameters
here, just empty parameter list for simplicity):
  ComChannel= com.open()

Now I want that the user can write e. g. "hallo" in two ways:
  Com.write( ComChannel, 'hallo')
  ComChannel:write( 'hallo')

... for this to work, I have to duplicate "meta functions" also to the
standard function list of the com library,  so the three tables in the
last post would correct to:

    // the "lib functions" without colon notation
    // (e. g. com.open, but also com.write( ComChannel, str) ...):
    static const luaL_Reg comlib[] = {
        {"open", com_open},
        {"read", com_read},
        {"write", com_write},
        {"close", com_close},
        {NULL, NULL}
    };

   //the "lib meta functions" with colon notation
   //(e. g. ComChannel:write(str) ...)
    static const luaL_Reg commeth[] = {
        {"read", com_read},
        {"write", com_write},
        {"close", com_close},
        {NULL, NULL}
    };

    // the "lib meta internal functions", like __gc etc... .
    // (this did not change, same as in my last post...)
    static const luaL_Reg metameth[] = {
        {"__index", NULL},  /* place holder */
        {"__gc", com_gc},
        {"__close", com_gc},
        {"__tostring", com_tostring},
        {NULL, NULL}
     };

So my functions for colon AND dot notation are now in BOTH first tables.

@Lua lib specialists: Is this normal? Or am I somehow "standing on the
line" somewhere and could get more easy / more straight forward?