lua-users home
lua-l archive

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


On Mon, Mar 24, 2014 at 08:41:22PM -0400, Rena wrote:
>    On Mar 24, 2014 6:34 PM, "Hisham" <[1]h@hisham.hm> wrote:
<snip>
>    > who really likes a disciplined, typed world
<snip>
>    I feel like format strings are one place where coercion is not a bad
>    thing. It's all about making concise code that's easy to read and
>    understand. When you write string.format("%d", x) it's clear that you
>    intend x to represent a number, so having an implicit tonumber() around
>    it makes sense. (Plus if x is, say, a table, an implicit conversion can
>    throw an "expected number, got table" error,while if I'm remembering
>    correctly tonumber(x) would return nil, making it harder to track down
>    the bug.)
> 
>    In fact I don't see why such coercion is a bad thing in most cases,
>    since context usually exists. You know that the operands to + must be
>    numbers and .. must be strings.
> 

All I know is that at work I always had to patch the Lua 5.1 VM to
implicitly call tostring on %s arguments.

I personally prefer stronger typing, but most of the engineers found the 5.1
behavior infuriating.

So I'm happy 5.2 and 5.3 invoke the tostring metamethod by default. I
imagine that I would receive similar complaints if the format specifier %d
didn't also coerce strings to numbers, especially considering that (%d+)
pattern matches return strings.