Weirdness from 'io.write()'

classic Classic list List threaded Threaded
7 messages Options
Reply | Threaded
Open this post in threaded view
|

Weirdness from 'io.write()'

Aaron Krister Johnson
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.

This seems a bug, and from what I can tell, it relates to the 'f_tostring' call in 'liolib.c'

I tried the same call in lua5.1.5, and it works as expected:

```
> io.write("A nice output msg")
A nice output msg> 
```

Please advise, thanks!

Warmly,

Aaron Krister Johnson
http://www.untwelve.org
Reply | Threaded
Open this post in threaded view
|

Re: Weirdness from 'io.write()'

nobody
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

Reply | Threaded
Open this post in threaded view
|

Re: Weirdness from 'io.write()'

Aaron Krister Johnson
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 <[hidden email]> 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

Reply | Threaded
Open this post in threaded view
|

Re: Weirdness from 'io.write()'

Shmuel Zeigerman
On my machine with Lua 5.3.5, the following
 > io.write("A nice output msg")
does not append anything.

But if it starts with an equality sign then it does append i.e. "file
(74ef4620)":
 > =io.write("A nice output msg")

I think it is mentioned in the manual.

Reply | Threaded
Open this post in threaded view
|

Re: Weirdness from 'io.write()'

Gabriel Bertilson
In reply to this post by Aaron Krister Johnson
Right, the 5.1 REPL doesn't print the values of expressions and you
therefore can't enter expressions on their own to see their values.
(They have to be preceded by an equals sign or "return", otherwise if
they can't be interpreted as statements, there is a syntax error.) I
found that very annoying since I'm used to the JavaScript console,
which allows you to do this. However, it is kind of counterintuitive,
in the REPL, that entering file:write() shows a return value.

As for documentation, the behavior of the Lua 5.3 REPL is documented
as follows[1]: "In interactive mode, Lua repeatedly prompts and waits
for a line. After reading a line, Lua first try to interpret the line
as an expression. If it succeeds, it prints its value. Otherwise, it
interprets the line as a statement. If you write an incomplete
statement, the interpreter waits for its completion by issuing a
different prompt. "

The 5.1 manual just includes the last sentence.[2]

The documentation of io.write in the 5.3 manual points you to
file:write(), whose documentation mentions the return value[3]: "In
case of success, this function returns file. Otherwise it returns nil
plus a string describing the error. "

But the Incompatibilities section for of the 5.3 manual doesn't
mention the change in the REPL,[4] and the 5.1 manual doesn't mention
the return value of file:write() (which seems to be a boolean).[5]

— Gabriel

[1] https://www.lua.org/manual/5.3/manual.html#7
[2] https://www.lua.org/manual/5.1/manual.html#6
[3] https://www.lua.org/manual/5.3/manual.html#pdf-file:write
[4] https://www.lua.org/manual/5.3/manual.html#8
[5] https://www.lua.org/manual/5.1/manual.html#pdf-file:write

— Gabriel



On Tue, Dec 31, 2019 at 5:37 PM Aaron Krister Johnson
<[hidden email]> wrote:

>
> 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 <[hidden email]> 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
>>

Reply | Threaded
Open this post in threaded view
|

Re: Weirdness from 'io.write()'

Shmuel Zeigerman
In reply to this post by Shmuel Zeigerman
On 01/01/2020 1:51, Shmuel Zeigerman wrote:

> On my machine with Lua 5.3.5, the following
> > io.write("A nice output msg")
> does not append anything.
>
> But if it starts with an equality sign then it does append i.e. "file
> (74ef4620)":
> > =io.write("A nice output msg")
>
> I think it is mentioned in the manual.
>
>

My bad. Please disregard the quoted message.
Sorry for the noise.

--
Shmuel


Reply | Threaded
Open this post in threaded view
|

Re: Weirdness from 'io.write()'

nobody
In reply to this post by Gabriel Bertilson
On 01/01/2020 01.10, Gabriel Bertilson wrote:
> But the Incompatibilities section for of the 5.3 manual doesn't
> mention the change in the REPL,

The REPL / standalone Lua interpreter is just a simple example
program(*), the important part of the release is the language / runtime.
As such, there are no incompatibilities / changes _in the language_
introduced by this.  Consequently, none are mentioned in that section.

-- nobody

(*) - I somewhat disagree with that, but that's the official stance, IIRC.