[Date Prev][Date Next][Thread Prev][Thread Next]
- Subject: A "subtype" module
- From: Dirk Laurie <dirk.laurie@...>
- Date: Sun, 12 May 2013 08:29:13 +0200
A subtype is a class of objects that share the same metatable, in
particular when the objects belong to a type of which the metatable
can only be set via the debug library.
I have written a module `subtype.c` of which the complete code is this:
/* A subtype is a standard Lua type with a subtype-specific metatable.
subtype = require"subtype"
bits = subtype('number',bits_mt)
j = bits(10)
0x0000000a number true
10 number nil
utf8 = subtype('string',utf8_mt)
t = "skêr"
s = utf8(t)
5 string true
4 string false
I.e. the effect is as if one said "debug.setmetatable(j,bits_mt)"
and "debug.setmetatable(s,utf8_mt)" but without affecting
other numbers and strings.
That's right, it consists of only a comment. The reason is that
I have only a foggy notion of what to do next. Clearly the real
object must be a userdata wrapping a single Lua TValue. Also
the built-in type function would need to be replaced. But it seems
to be impossible to do what I want in the C API, one would need
to use lobject.h.
Am I missing something?