lua-users home
lua-l archive

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


On Fri, Sep 22, 2000 at 07:26:33PM +0900, John Belmonte wrote:
> Why not have lua_type return an enumeration of the types?  This will
> eliminate the "dirty trick" (which everyone will certainly do instead of
> messing with string compares).  If it's believed that strings are really
> necessary for some reason, an additional function could be added to convert
> the type enum to a string.

Another "dirty trick" for checking the type of a value is to check its tag 
value. The types nil, number, and string each have predefined tags, functions
can have one of two possible tag values. Tables are also given a pre-defined
tag but can be changed. I assume the same applies to userdata as it does to
tables.

	for i, v in numberlist
		--if type(v) == "number" then end
		if tag(v) == 1 then end
	end

>From lobject.h:
typedef enum {
  TAG_USERDATA  =  0, /* default tag for userdata */
  TAG_NUMBER,	/* fixed tag for numbers */
  TAG_STRING,	/* fixed tag for strings */
  TAG_TABLE,	/* default tag for tables */
  TAG_LCLOSURE,	/* fixed tag for Lua closures */
  TAG_CCLOSURE,	/* fixed tag for C closures */
  TAG_NIL,	/* last "pre-defined" tag */

  TAG_LMARK,	/* mark for Lua closures */
  TAG_CMARK	/* mark for C closures */
} lua_Type;

I don't have much experiencing in profiling code but in my rudimentary attempts
I found the code executed ~3% faster (a large for loop similar to my example 
above).

-Lenny