lua-users home
lua-l archive

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



> Am 08.05.2020 um 11:04 schrieb Sean Conner <sean@conman.org>:
> 
> It was thus said that the Great Marc Balmer once stated:
> 
>> Many of the Lua modules we wrote from scratch or adapted from existing
>> software are published as open source software on
>> https://github.com/arcapos/ <https://github.com/arcapos/>.  They are all
>> maintained and in wide use in our commercial products (mostly point of
>> sale and online systems).  They lacked, however, documentation in most
>> cases.
> 
>  I'm reading the code for UUID: https://github.com/arcapos/luauuid/blob/master/luauuid.c
> and I was struck by an odd choice.  The code goes:
> 
> 	struct luaL_Reg uuid_methods[] = {
> 		{ "clear"   , lua_uuid_clear },
> 		{ "compare" , lua_uuid_compare },
> 		...
> 	};
> 
> 	if (luaL_newmetatable(L,UUID_METATABLE)) {
> 		luaL_setfuncs(L,uuid_methods, 0);
> 
> 		...
> 
> 		lua_pushliteral(L,"__eq");
> 		lua_pushcfunction(L,lua_uuid_equal);
> 		lua_settable(L,-3);
> 
> 		...
> 	}
> 
>  Why not just list the metamethod functions in uuid_methods[]?
> 
> 	struct luaL_Reg uuid_methods[] = {
> 		{ "__eq"       , lua_uuid_equal },
> 		{ "__lt"       , lua_uuid_less },
> 		{ "__le"       , lua_uuid_less_or_equal },
> 		{ "__gc"       , lua_uuid_clear },
> 		{ "__tostring" , lua_uuid_unparse },
> 		{ "__concat"   , lua_uuid_concat },
> 		{ "__len"      , lua_uuid_length },
> 		{ "clear"      , lua_uuid_clear },
> 		{ "compare"    , lua_uuid_compare },
> 		...
> 	};
> 
>  Saves some code.  You still have to push the __index and __metatable
> fields, but the functions can be set along with the other functions.

Thank you for the suggestion, and yes indeed, that is nicer...

> 
>  And because it hurts me to have to type
> 
> 	syslog('warning',string.format("Unit %d is at %s",unit,status))
> 
> you can have the syslog function call format for you:
> 
> 	static int
> 	unix_syslog(lua_State *L)
> 	{
> 		luaL_checktype(L,1,LUA_TSTRING);
> 		lua_getfield(L,2,"format");
> 		lua_insert(L,2);
> 		lua_call(L,lua_gettop(L) - 2,1);
> 		syslog(priorities[luaL_checkoption(L,1,NULL,priority_names)],"%s",lua_tostring(L,-1));
> 		return 0;
> 	}
> 

That's also a good idea.

Thanks for the feedback!

- mb
_______________________________________________
lua-l mailing list -- lua-l@lists.lua.org
To unsubscribe send an email to lua-l-leave@lists.lua.org