lua-users home
lua-l archive

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

2012/11/15  <>:

> How about the option of declaring variables of a specific type?

I'm not clear in my mind whether you mean lexical typing or
table/userdata subtyping as envisaged by the subject of
the thread.

If you mean lexical typing. i.e. associating a name permanently
with a certain type, you get an interesting language: for instance,
I assign something to a variable that has been typed "string".
Is it a run-time error if the object is not of type "string"?
Is coercion to string automatically invoked? Or can the user
choose by calling some control routine?

An interesting language, yes — but the language is no longer
Lua.  Lua has names and values, you can associate any
value with any name, and you can kill any name by associating
the value "nil" with it.

If you mean that type(obj) should return different names for
objects of different kinds, it is easy to do that oneself.

At the top of your program, put this:

_rawtype = type
_type = {}
type = function(val)
   return _type[getmetatable(val)] or _rawtype(val)

When creating an object, somewhere there is a line


Change this to

_type[mt] = "mytype"