lua-users home
lua-l archive

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

In message <>
          Dirk Laurie <> wrote:

> A patch that makes a parameter list
>   (x:number)
> syntactic sugar for
>   (x) assert(type(x)=='number')
> would be easy, has anyone done it already?

I sometimes use

 the = function (TYPE, VALUE)
       assert (type (VALUE) == TYPE)
       return VALUE

OK, a statement like
   f (the ("number", x), the ("string", s))
is more clumsy than f ((x:number), (s:string)).

Another alternative is to do

  is = setmetatable ({ }, {
       __index = function (_, TYPE)
                 return function (VALUE)
                 assert (type (VALUE) == TYPE)
                 return VALUE
                 end end })

and use
   f (is.number (x), is.string (s))
which looks a lot better, without any patching.
Gavin Wraith (
Home page: