lua-users home
lua-l archive

Re: how to prevent bugs without static type checking?

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


On 25/10/2012, at 8:43 AM, eugeny gladkih <john@gladkih.com> wrote:
>> --- prints the string s n times
>> -- @tparam number n how many times to print the string
>> -- @tparam string s the string to print
>> function printn(s, n)
>> for i = 1, n do print(s) end
>> end
>> 
>> printn(10, "hello")
> 
> what about
> 
> printn("hello",'10')
> 
> ?
As in, does the "number" constraint check whether tonumber (or automatic type coercion) can coerce the argument into a number? Currently, no, it just does 'type(value) == constraint'. It'd be easy to change, but I get the feeling automatic coercion is not always a popular feature.
But you can go:
function printn(
 s, -- string
 n) -- number|string
or, for that matter
function printn(
 s, -- string
 n) -- 1..640: 640 ought to be enough for anybody
and so on.

AltStyle によって変換されたページ (->オリジナル) /