lua-users home
lua-l archive

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


在 2016/7/4 18:41, Viacheslav Usov 写道:
On Mon, Jul 4, 2016 at 4:19 AM, 书呆彭, Peng Yi <nerditation@outlook.com <mailto:nerditation@outlook.com>> wrote:

> the fact that `lua_tolstring` also accept a number value is just a side effect of the way Lua coerce number and string.

Fair enough. Then the question is, why is coercion of Boolean into string is not part of the language?

Unless I am missing something, that seems rather arbitrary to me.

Cheers,
V.

well I don't know the exact rationale about the coerce, but it's what Lua is currently like.
I guess it was there for convenience. also there were many discussion on this topic before.[*]


personally I always use `tostring` and `tonumber` explicitly in my own code. admittedly it
could feel verbose and even boring at times, but I'm OK with it.


for example, this is legal in Lua:
```
    a, b = 1, 2
    c = a .. b

    x, y = '3', '4'
    z = x + y
```

but I always do it this way:
```
    c = tostring(a) .. tostring(b)
    z = tonumber(x) + tonumber(y)
```

or like this:
```
    a, b = tostring(a), tostring(b)
    c = a .. b
    x, y = tonumber(x), tonumber(y)
    z = x + y
```



[*] http://lua-users.org/lists/lua-l/2015-03/msg00322.html
[*] http://lua-users.org/lists/lua-l/2014-07/msg00534.html

--
the nerdy Peng / 书呆彭 / Sent from Thunderbird