lua-users home
lua-l archive

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


Name: weak_type
Version: 0.2
Link: https://github.com/andrewstarks/weak_type.git
Lua Version: 5.2
License: Public Domain (Unlicense)
Author: Andrew Starks

Summary: Weak Type is an extension to Lua's type system, which allows
the programmer to get more detail about an object's type, while not
breaking common idioms or compatibility with Lua.

Long Description:

I've been spending some time (that I don't have) on Weak Type, an
experiment in a type library that I was playing with and am now using:

By way of example:

```
type = require'weak_type'

type(nil)
-->nil

type({})
-->table
-
-this is different.
--type()
-->undefined, nil

my_obj = {}

= type(my_obj, "table")
-->table, table
obj_type_t = type.new{"my_obj_type"}

type.cast(my_obj, obj_type_t)

= type(my_obj, "table")
-->my_obj_type, table


int64_t = type.new{"int64", "number", userdata = true}
int64 = require'int64'

int3 = int64.new(3)
int64_t(int3)

= type.tostring(int3)
-->int64, number
=type(int3, 'userdata')
-->int64, userdata

= type.get(3) <= int64_t
-->true
```
New in this version:

* My first attempt was far too complicated and therefore broken. Now I
don't automatically find types by string name arguments, which made it
pretty much impossible to test for a string. :)
* First support for userdata, provided the debug library is available
and the module author stored the metatable in the registry.
* Caching of userdata and native value types.
* Equality / Containment operators are much more efficient and are
also not broken (as far as I know).
* Equality tests for all values types.
* Changed "assignment" to "cast"
* Added "find" function, which simply returns a stored type by its name.
* Made many more tests and added a great deal of documentation. Next
up: ldoc. :)

Findings:

For me, this is actually really nice to work with. I find myself
appreciating real names for objects that I pass around, along with the
fact that I can easily test if it's a table. In practice, I type (no
pun here) less and my debugging is easier.

I don't know about speed, but I haven't noticed any difference. Tables
are created when types are made. No tables are made during any type
checking, however.


Any and all comments would be very much welcome.

-- Andrew Starks