lua-users home
lua-l archive

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


I like this a lot.

One possible extension is to follow the lead of luaL_checkudata surfacing
this test in Lua. So if a type is defined using a userdata with a metatable
stored under a type name in the registry (as the C API encourages):

type(x, "mytype")

checks if type is a userdata or table with a metatable equal to the table
stored in the registry under the key "mytype".

This allows for the test to work even if a "type" metavalue is not defined.

> Date: Sat, 10 Aug 2013 01:32:05 -0500
> From: Andrew Starks <andrew.starks@trms.com>
> Subject: [ANN] Weak Type: A type library for the weak (hack stage)
> To: Lua mailing list <lua-l@lists.lua.org>
> 
> Weak Type is a small type library that I made. It can be found here:
> 
> https://gist.github.com/andrewstarks/6199229
> 
> ````
> 
> local type = require'weak_type'
> 
> -- Works like normal.
> print(type(nil))
> --> nil
> print(type("Hello!"))
> --> string
> print(type({}))
> --> table
> 
> -- Additional arguments are type names.
> print(type({}, "number"))
> --> false
> print(type(false, "boolean"))
> --> boolean, boolean
> 
> -- make a type and assign it to an object (any table)
> -- this will clobber the __type field in your metatable. It will
> -->also make a metatable if you need one.
> 
> local o = type.assign({}, {"my_object_type", "number"})
> 
> -- Remember, weak_type works like normal type
>  print(type(o))
> -- table
> 
> --To get the type name, you can do a number of things:
> print(type.tostring(o))
> --> my_object_type
> print(type.check(o))
> --> my_object_type
> print(type.get(o))
> --> my_object_type (a type object that can be compared with other
> types, it `tostring`s to this, however.)
> 
> -- If an object can impersonate a number, this construct is nice.
> print(type(o, "number"))
> -->my_object_type, number -- first the primary name, then the match.
> 
> -- What if we want to know if it's an object or a table?
> print(type(o, "my_object_type", "table"))
> --> my_object_type, my_object_type
> print(type({}, "my_object_type", "table"))
> --> table, table
> 
> local o2 = type.assign({}, {"another_type", "number", "foo",
> "my_object_type"})
> 
> print(type.is_contained(o, o2))
> --> true
> print(type.is_contained(o2, o))
> --> false
> 
> ````
> There's more. Instructions, such as they are, can be found in the text
> surrounding the tests at the bottom of the file.
> 
> Some met goals were:
> 
> - doesn't create tables when types are checked.
> - works like type, except that if it doesn't receive any arguments it
> doesn't error:
> ````
> print(type((function() return end)()))
> --> nil, invalid key
> ````
> - works for my needs. :)
> 
> My goal in posting this is much more about getting your constructive
> criticisms than a belief that this is a unique contribution to the Lua
> world. Of course, pointing out the parts that I got right is nice,
> too. :)
> 
> I'm still quite inexperienced and daylight has a way of (sometimes
> painfully) helping to educating.
> 
> Thanks!
> 
> -Andrew
> 
> 
> 
> ------------------------------
> 
> Message: 14
> Date: Fri, 9 Aug 2013 23:58:16 -0700
> From: Hao Wu <wuhao.wise@gmail.com>
> Subject: string literal following by colon-style call?
> To: Lua mailing list <lua-l@lists.lua.org>
> Message-ID:
> 	<CAH+P0Kuz267a-owFzHWQrvnoxKrnJgd3GvgRzBy-
> _=FE736fJg@mail.gmail.com>
> Content-Type: text/plain; charset="utf-8"
> 
> SGkgbGlzdCwKClN0cmluZyBoYXMgaXRzIG1ldGF0YWJsZSwgc28gZm9yIHRoZSBmb2xsb3d
> pbmcg
> Y29kZToKCmxvY2FsIHN0ciA9ICJUaGlzIElzIGEgU3RyaW5nIgoKd2UgY291bGQganVzdCB
> zYXkK
> CmZvciB3IGluIHN0cjpnbWF0Y2goIiV3IikgZG8gcHJpbnQodykgZW5kCgpTbyBteSBxdWV
> zdGlv
> biBpcywgd2hlbiBkZWFsaW5nIHdpdGggc3RyaW5nIGxpdGVyYWwsIHdoeSBjb3VsZG4ndCB
> 3ZSBq
> dXN0CnNheToKCiJhIHN0cmluZyBsaXRlcmFsIjpsb3dlcigpCgppbnN0ZWFkCgooImEgc3R
> yaW5n
> IGxpdGVyYWwiKTpsb3dlcigpCgpJcyB0aGVyZSBhIGxhbmd1YWdlIHJlYXNvbiB0byBwcmV
> 2ZW50
> IHRoaXM/CgpUaGFua3MsCgp+aGFvCi0tLS0tLS0tLS0tLS0tIG5leHQgcGFydCAtLS0tLS0
> tLS0t
> LS0tLQpBbiBIVE1MIGF0dGFjaG1lbnQgd2FzIHNjcnViYmVkLi4uClVSTDogaHR0cDovL3Z
> saXN0
> cy5wZXBwZXJmaXNoLm5ldC9jZ2ktYmluL21haWxtYW4vcHJpdmF0ZS9sdWEtbC1saXN0cy5
> sdWEu
> b3JnL2F0dGFjaG1lbnRzLzIwMTMwODA5L2M1ODM4NDdmL2F0dGFjaG1lbnQtMDAwMS5odG1
> sCg==
> 
> ------------------------------
> 
> Message: 15
> Date: Sat, 10 Aug 2013 00:31:41 -0700
> From: Tim Hill <drtimhill@gmail.com>
> Subject: Re: string literal following by colon-style call?
> To: Lua mailing list <lua-l@lists.lua.org>
> Message-ID: <26F227C8-90F9-41D4-92A8-9B85BC350F7E@gmail.com>
> Content-Type: text/plain; charset=us-ascii
> 
> 
> On Aug 9, 2013, at 11:58 PM, Hao Wu <wuhao.wise@gmail.com> wrote:
> 
> >
> > So my question is, when dealing with string literal, why couldn't we
> just say:
> >
> > "a string literal":lower()
> >
> > instead
> >
> > ("a string literal"):lower()
> >
> > Is there a language reason to prevent this?
> >
> > Thanks,
> >
> > ~hao
> 
> What's more interesting is that, by my reading of the BNF, _neither_ of
> these should work (though the second, as you state, does work). SO far
> as I can see, the BNF doesn't describe the full colon syntax (it is
> only mentioned when used as part of a function declaration).
> 
> --Tim
> 
> 
> 
> 
> ------------------------------
> 
> _______________________________________________
> lua-l mailing list
> lua-l@lists.lua.org
> http://vlists.pepperfish.net/cgi-bin/mailman/listinfo/lua-l-
> lists.lua.org
> 
> 
> End of lua-l Digest, Vol 37, Issue 15
> *************************************