lua-users home
lua-l archive

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


Reuben Thomas wrote:
> 
> f = fun ({Int, String},
> function (i, s)
>   print s .. " " .. tostring (i)
> end)
> 
> so that it's a bit nicer to write.
> 
> Has anyone else tried something like this?

Yes, but not like that.  I thought about a compiler extension.
An optional type name before the parameter name.  Like:

  function(typename varname, ...)

The actual check is done by looking for an is_<typename> in
the method table of the object passed to varname.  The compiler
would generate a single instruction for each checked parameter.
One could even go further and check each assignment made to
that name with the same instruction.  Another step could be
to allow the same extension on any variable declaration.

The advantage of the is_<typename> lookup would be that it could
work on inherited types too.  Of course, all types (including
numbers, strings, ...) would need a method table...

Ciao, ET.

PS: I think, one needs another syntax to specify optional
typed arguments (may be nil or of that type).  I.e. append
an '?': function(FooType x?)

PPS: %s/method table/metatable/