lua-users home
lua-l archive

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




On Monday, March 24, 2014, William Ahern <william@25thandclement.com> wrote:
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.



Pattern matching is not confusingly-related to plain old coercion. You're dealing with string patterns, after all, or am I misunderstanding the context of %d+?

I agree that number to string should always be "for free". I also agree that ".." should always call __tostring, even on (especially) tables.

It's string to number. Strings are often used as magic words or alternates types in arguments and it's better if things are clearer. 

Might be better to relegate bad behavior... I mean implicit coercion :) to a metamethod.

There's nothing wrong with a library supported  __tonumber metamethod, in my book. One could always patch the string library if they would want it back. 

 I just argued against myself, didn't I...

-Andrew