lua-users home
lua-l archive

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


I guess that makes sense..? Will try later.

I still contend that:

1) it doesn't work as advertised in the documentation. The docs do not mention this, that I have seen.

2) assuming that the above is true, the docs for lua5.3.* need updating, and this behavior needs explanation.

3) This behavior doesn't seem to be the case for the REPL prior to 5.3, per my statement that 5.1.5 worked as the docs state, w/o return values being printed.

Personally, I like the old behavior better. :)

-A


On Tue, Dec 31, 2019, 3:07 PM nobody <nobody+lua-list@afra-berlin.de> wrote:
On 31/12/2019 21.45, Aaron Krister Johnson wrote:
> Hi all,
>
> I am curious why 'io.write()' isn't behaving as advertised in lua5.3.*:
>
> ```
>  > io.write("A nice output msg")
> A nice output msgfile (0x7f8563013760)
>  >
> ```
>
> In particular, notice the "file (...)" bit that gets appended.

io.write works correctly, it only looks wrong in the REPL.  This is
resulting from the interplay of several features:

First, io.write always returns the file handle, so you can chain writes:

   io.write "foo" :write "bar" :write "etc.…"

Second, the REPL prints all return values.  'return 2+2' prints 4.
Saying 'return io.write "foo"' will result in exactly the behavior that
you observed. (This should also work in 5.1 IIRC?)

Third, newer versions automatically try to prepend 'return ' to anything
you type at the REPL.  (So typing '2+2' no longer throws an error but
instead says '4'.)

In combination,

   > io.write "foo"

will be read as 'return io.write "foo"', which prints "foo" and then the
return value(s) – in this case the file handle.

Try putting a semicolon in front of your statement / _expression_.
'return; io.write "foo"' isn't valid, which will result in it being
parsed as a statement (which doesn't return anything, and thus prints no
extra values).

-- nobody