lua-users home
lua-l archive

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


In message <CABcj=t=jokyccrw18fSEuwVeEDoSPqE7jE-2vWDJOf1UaANv-Q@mail.gmail.com>
          Dirk Laurie <dirk.laurie@gmail.com> 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
       end

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 (gavin@wra1th.plus.com)
Home page: http://www.wra1th.plus.com/